Reputation: 12512
I would like to have a php function that will strip any input and keep only a numeric ID, 36816268 in the example below.
The input can be something like this:
<iframe src="http://player.vimeo.com/video/36816268" width="400" height="225" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>
or like this
<iframe src="http://player.vimeo.com/video/36816268" width="400" height="225" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe><p><a href="http://vimeo.com/36816268">ABCinema</a> from <a href="http://vimeo.com/eeseitz">Evan Seitz</a> on <a href="http://vimeo.com">Vimeo</a>.</p>
I can strip the first part as
preg_match('%.*http://player.vimeo.com/video/%im', $subject)
Upvotes: 0
Views: 107
Reputation: 375
You will have to match the id in a group and replace the whole string with just the ID.
Example script:
<?php
$string = '<iframe src="http://player.vimeo.com/video/36816268" width="400" height="225"
frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>';
echo preg_replace('#^.*video/(\d+).*$#i', '$1', $string); // prints '36816268'
?>
Explanation of the pattern:
#
start pattern^
start match at the beginning of the string.*
match anything, except newlinevideo/
match string video/(
start capturing group, group 1\d+
match one or more digits)
close capturing group.*
match anything$
matches the end of the string#
end patterni
makes the match case insensitiveUpvotes: 0
Reputation: 11690
$str = '<iframe src="http://player.vimeo.com/video/36816268" width="400" height="225" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>';
preg_match('#src=\"[^\d]+(\d+)\"#Ui', $str, $m);
print_r($m);
Upvotes: 0
Reputation: 270637
Since I can't find a suitable duplicate to link (knowing similar questions have been asked many times), I'll put it in as an answer. It is quite easy as long as the rest of the URL doesn't need to change.
$matches = array();
preg_match("~.*http://player.vimeo.com/video/(\d+)~im", $subject, $matches);
print_r($matches);
echo $matches[1];
Array
(
[0] => <iframe src="http://player.vimeo.com/video/36816268
[1] => 36816268
)
Upvotes: 1