Any questions involving matching text strings to patterns - the pattern is called a "regular expression."
Moderator: General Moderators
-
tores
- Forum Contributor
- Posts: 120
- Joined: Fri Jun 18, 2004 3:04 am
Post
by tores »
Hi
How can I extract all sources of each frame-tag. E.g if I had the following two frame tags in some html.
Code: Select all
<frame name="right" src="right.html" scrolling="no">
<frame name="left" src="left.html" scrolling="no">
I want the regexp to return right.html and left.html...
I guess something like
Code: Select all
preg_match('[magic regexp]', $html, $matches);
should do the trick
-
McGruff
- DevNet Master
- Posts: 2893
- Joined: Thu Jan 30, 2003 8:26 pm
- Location: Glasgow, Scotland
Post
by McGruff »
If you can rely on the html being written exactly as above, use look ahead/behind:
Code: Select all
/(?<=<frame\s+name="right"\s+src=")[^"]+(?=")/i
It will break if the name/src order changes.
-
Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Post
by Chris Corbyn »
... whereas this one won't break so long as they are frame's
Code: Select all
function getFrameSrc($code)
{
preg_match_all('/<frame [^>]*?\bsrc="([^"]+)"[^>]*>/is', $code, $matches);
return $matches[1]; //Uni-dimensional array of src's
}