Re: preg_match html tags
| From: | laurence | Date: | Wed, 02 Mar 2005 21:45:03 +0000 |
| Subject: | Re: preg_match html tags | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-38743@lists.php.net to get a copy of this message | ||
Do it with one preg_match operation, like so:
preg_match("/(?<=Purpose<p>).+?(?=<\/p>)/", $fpRead, $matches_new);
Your string will reside in $matches_new[0].
EXAMPLE:
$fpRead = "blah blah Purpose<p>This is the string you are after</p> blah
blah <p>blah blah blah</p>";
preg_match("/(?<=Purpose<p>).+?(?=<\/p>)/", $fpRead, $matches_new);
print_r($matches_new);
EXPLANATION:
(?<=Purpose<p>)
- backward looking assertion. Read as: "Expression immediately following
must match only strings with "Purpose<p>" immediately before."
.+?
- any character, 1 or more, ungreedy. Ungreedy because otherwise it will
match right up to the last possible place it can. You want it to match up
until the next '</p>', not the last '</p>'.
(?=<\/p>)
- forward looking assertion. Read as: "Expression immediately prior must
match only strings with "</p>" immediately after."
Hope this helps.
Laurence
"Kathy A Wright" <Kathy_A_Wright@Keane.com> wrote in message
news:OF3C6E80D4.E9500592-ON85256FB8.006EE725-85256FB8.006F6C79@keane.com...
>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]);
>
> 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 ]
>