こんにちは私はYouTubeのリンクを埋め込みコードに変換しようとしています。preg_replace()で埋め込むすべてのYouTube URLを交換してください
これは私が持っているものです。
<?php
$text = $post->text;
$search = '#<a(.*?)(?:href="https?://)?(?:www\.)?(?:youtu\.be/|youtube\.com(?:/embed/|/v/|/watch?.*?v=))([\w\-]{10,12}).*$#x';
$replace = '<center><iframe width="560" height="315" src="http://www.youtube.com/embed/$2" frameborder="0" allowfullscreen></iframe></center>';
$text = preg_replace($search, $replace, $text);
echo $text;
?>
それは一つのリンクのために働きます。しかし、2つを追加すると、最後のオカレンスだけが交換されます。私は何を変えなければならないのですか?
感謝を!私はそれらの正規表現の記号すべてでそれを見逃しているに違いない。 – Axel
確かに、正規表現ドメインの欲張りの意味も分かりません。 – hakre