Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

XML String Length

  • 06-09-2007 11:00AM
    #1
    Closed Accounts Posts: 230 ✭✭


    Hi Guys,

    Using the following code to parse some XML but it wont return anthing thats longer than a line of txt. The xml is created from html textareas on a php page. All works fine until there is a new line in the text. Anyone any ideas :confused:

    $itemtitle = preg_match_all("/<".$xl[$count2].">(.*?)<\/".$xl[$count2].">/", $data, $match);
    $replace = array('<![CDATA]>'=>'');
    $_SESSION[$names[$count2]]= strtr($match[1][0],$replace);

    Dan


Comments

  • Registered Users, Registered Users 2 Posts: 1,022 ✭✭✭[CrimsonGhost]


    Okay, firstly if you are doing to work with XML don't try regular expression matching to get what you want. It's slow and there are plenty of easier ways.

    I don't know how $data looks but if it is for example
    <data>
    <title></title>
    <name></name>
    </data>

    You can access it's parts much more simply with

    $simpleData = simplexml_load_string($data);
    $title = $simpleData->title
    $name = $simpleData->name


    Check out http://ie.php.net/simplexml for more details, but you'll find it much much easier for working with XML than what you have. And you won't have problems with it only handling a single line.


  • Closed Accounts Posts: 230 ✭✭danindublin


    Okay, firstly if you are doing to work with XML don't try regular expression matching to get what you want. It's slow and there are plenty of easier ways.

    I don't know how $data looks but if it is for example
    <data>
    <title></title>
    <name></name>
    </data>

    You can access it's parts much more simply with

    $simpleData = simplexml_load_string($data);
    $title = $simpleData->title
    $name = $simpleData->name


    Check out http://ie.php.net/simplexml for more details, but you'll find it much much easier for working with XML than what you have. And you won't have problems with it only handling a single line.

    Thanks CrimsonGhost,

    The only reason I've been using this more complicated version is because I'm working off a server that only has php version 4.4 whereas simpleXML needs v5+.


  • Registered Users, Registered Users 2 Posts: 1,022 ✭✭✭[CrimsonGhost]


    Ahh, right. You might tell them PHP5 when production over 3 years ago and support for php4 ends at the end of the year. (Ref: http://www.php.net/#2007-07-13-1).

    As a possible solution, a bit hacky mind, could you do a preg_replace before your match and replace all "\n" with "", should give you the whole thing as a single line. It may run a little slower but should give you a result quick enough.


  • Registered Users, Registered Users 2 Posts: 3 eweb


    In perl . doesn't match end of lines. So either add an 's' modifier after the final / or replace end of lines with a space.


Advertisement