Any way to pull out a string between two tags?

Discussion in 'PHP' started by Roze, Nov 2, 2005.

  1. #1
    I have parsed some RSS feeds into a database. Yes I'm using carp evolution, no i cant just aggregate them.

    One of the feeds is formatted annoyingly wrong, so when I print out the description from the DB it has all this junk on it :

    Author: Craig
    Subject: Mirafone S-186 value?
    Posted: Wed Nov 02, 2005 5:58 pm (GMT -5)
    Topic Replies: 0
    <span class="postbody">WHAT I WANT IS HERE</span>

    What I want is to show is the text between the SPAN tags. Are there any php functions i can apply to only get the contents of those span tags?

    I've looked at ereg_replace and preg_replace and strip tags and all sorts of php tricks, no dice. Thanks in advance for a hint!
    -R
     
    Roze, Nov 2, 2005 IP
  2. daniah

    daniah Well-Known Member

    Messages:
    1,092
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    138
    #2
    daniah, Nov 2, 2005 IP
  3. draculus

    draculus Peon

    Messages:
    63
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    if you are not up to regular expressions, this might be simpler.

    $start = strpos('<span class="postbody">', $thestring);
    $start = $start + 23;
    $end = strpos("</span>", $thestring);

    $thebitiwant = substr($thestring, $start, $end - $start);
     
    draculus, Nov 2, 2005 IP
  4. Amos

    Amos Peon

    Messages:
    224
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Preg match is your best choice, although I believe it's a little bit slower than draculus' example.
     
    Amos, Nov 2, 2005 IP