Re: preg_match html tags
| From: | Martin Norland | Date: | Wed, 02 Mar 2005 20:39:49 +0000 |
| Subject: | Re: preg_match html tags | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-38742@lists.php.net to get a copy of this message | ||
Kathy_A_Wright@Keane.com wrote:
I am trying to get a string of text in an html page using preg_match. The text starts after the Purpose</p> tag and ends before the next </p> tag. When using preg_match it captures the first </p> tag on the page and not the one after my starting point. Here is my starting point: preg_match("/Purpose\<\/p\>/i", $fpRead, $matches_new) Here is my ending point: preg_match("/<(\/p)>/s", $fpRead,$m_new); I have also tried using offset preg_match("/<(\/p)>/s", $fpRead,$m_new, $matches_new[0]);You want to match the second expression starting after that Purpose</p>. Use: preg_match("/<(\/p)>/s", $matches_new[0], $m_new); .. check the documentation on php.net for that function, you don't pass it an offset - it takes an optional flag to return the offset along with the matches. You may have been thinking of: strpos($haystack, $needle, [$offset]) - which is what I would recommend using for the first part (since you want the first occurrence), then using that with substr() to get the first occurrence of a closing </p> to get the string you want. [ warning - wholly untested code :) ] $start = strpos($fpRead, "Purpose</p>"); $end = strpos($fpRead, "</p>", $start); // make what we're doing clearer for the mailing list // also helps in case offsets are a little strange. $length = $end - $start; $what_i_want = $substr($fpRead($start,$length)); cheers, -- - Martin Norland, Sys Admin / Database / Web Developer, International Outreach x3257 The opinion(s) contained within this email do not necessarily represent those of St. Jude Children's Research Hospital.Does anyone have any suggestions? I also don't know if my syntax is correct for the string </p>...Thanks Any help will be appreciated. Kathy Kathy A Wright | Keane Inc. | Suite: Outside: 617-517-1706 | E-mail: kwright@keane.com [ Mailing: 100 City Sq. Charlestown, MA 02129 USA ]