Zuhair Taha
Zuhair Taha

Reputation: 3032

php regex get first youtube id form text

how to get:

WA4nNDfQ0wA

form this code:

<p>some text here ...</p>
<iframe width="640" height="360" src="//www.youtube.com/embed/WA4nNDfQ0wA?feature=player_embedded" frameborder="0" allowfullscreen></iframe>
<div>some text here ...</div>
<iframe width="640" height="360" src="//www.youtube.com/embed/9gypDWA6r2s?feature=player_embedded" frameborder="0" allowfullscreen></iframe>

by using php?

Upvotes: 1

Views: 258

Answers (1)

Sabuj Hassan
Sabuj Hassan

Reputation: 39355

Assuming your html is inside the $html variable.

preg_match("|.*?/embed/([\w-]+)|", $html, $m);
print $m[1];

If you change the .*? into .*(greedy) then it will match the last id.

Upvotes: 2

Related Questions