SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Nov 2006
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Regular expression help.

    Hello list,
    Could someone tell me how I could grab some text using a regexp but not include some of the criteria in the returned result.
    So taking a string like:

    $doc = "<body><p>here is some text.</p></body>"

    Using a regexp like:
    preg_match("/<body>.+<\/body>/is",$doc,$matches);

    But instead excluding the body tags from the returned result so that $matches[0] would be:

    "<p>here is some text.</p>"

    Thanks.

  2. #2
    SitePoint Evangelist hexburner's Avatar
    Join Date
    Jan 2007
    Location
    Belgium
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The first index (0) of the array $matches will always be the entire match.
    Here's how:
    PHP Code:
    $doc '<body><p>here is some text...</p></body>';
    preg_match('/<body>(.+)<\/body>/ismU'$doc$matches);
    echo 
    $matches[0]; // echoes <body><p>here is some text...</p>
    echo $matches[1]; // echoes <p>here is some text...</p> 
    FOR SALE: 1 set of morals, never used, will sell cheap


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •