Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

XML String Length

Options
  • 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 Posts: 1,023 ✭✭✭[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 Posts: 1,023 ✭✭✭[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 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