View Full Version : Help with stopping preg_match

05-18-2011, 08:17 PM
How would I go about getting this to stop at an "&" or a quotation mark?

else if (preg_match('#http://www.random.com/swf/(.+)#si', $match, $id))

I'm not sure what it does as is exactly, if that could be explained as well, thank you!

05-18-2011, 10:14 PM
This is the wrong forum, moving from snippets to PHP.
It captures any characters after /swf/. To skip, try using #http://www.random.com/swf/([^&\'"]+)#si.
Not sure why you're worried about quotations; those cannot be provided as a part of the url.

05-18-2011, 10:30 PM
Well it's for grabbing part of a video player embed. I want for it to grab anything after the /swf/, except stop once it gets to a " & " or a " " " while excluding them from the $id as well.

05-18-2011, 11:33 PM
Oh I see from a scrape or something of the sorts.
That should do it.

05-19-2011, 03:06 AM
That code doesn't seem to stop it at the first "&", it stops it at the second "&" though...

05-19-2011, 04:14 AM
That code doesn't seem to stop it at the first "&", it stops it at the second "&" though...

My test says otherwise:

$pattern = '#http://www.random.com/swf/([^&\'"]+)#si';
$str = 'this is a <a href="http://www.random.com/swf/teststuffs&a=a&b=b">an example</a> with a
link from http://www.random.com/swf/somextrastuffs&done=done in it.';

if (preg_match_all($pattern, $str, $matches))
printf("<pre>%s</pre>", print_r($matches, true));


[0] => Array
[0] => http://www.random.com/swf/teststuffs
[1] => http://www.random.com/swf/somextrastuffs

[1] => Array
[0] => teststuffs
[1] => somextrastuffs


The results are multidimensional since I used a preg_match_all instead. Preg_match will be single.

Unless there's something I'm missing from the criteria?

05-19-2011, 09:09 AM
That does seem to be the case...

$s = file_get_contents($VideoSite);

$patterns = array();
$patterns[] = '<embed[^>]+src="(.+?)"';
$patterns[] = '<iframe[^>]+src="(.+?)"';
$patterns[] = '<object[^>]+src="(.+?)"';

$patterns = "#(?:" . implode("|", $patterns) . ")#si";

preg_match_all($patterns, ($s), $m);
if (!empty($m[0]))
$edata = array();
foreach($m[0] as $match)
if (preg_match('#http://www.dailymotion.com/swf/(.+)"#si', $match, $id))
$edata[] = "[dailymotion " . $id[1] . "]";


Sorry, I didn't really think it was the other part of the code. Anyways I still don't know how to deal with it...