0

私はほとんどそこにいます!

これが私が調整しようとしている文字列と私の preg_replace の試みです。

$description_string = '<p>Here is the test link: <a href="http://www.youtube.com/watch?v=2MFn8L9tIrg" target="_blank">“Man or Muppet”&lt;/a> with other text afterwards.</p>';

$description = preg_replace( '/(<a[^>]+youtube[^>]*>)+[^"]*(<\/a>)+/', '$0Watch This Video$2', $description );

私が得ている結果は間違っています:

テスト リンクは次のとおりです。「Man or Muppet」後で他のテキストとともにこのビデオをご覧ください。

どんな助けでも大歓迎です!ありがとう!

4

1 に答える 1

0

実際のタイトルを取得しようとしているかどうかはわかりません。しかし、ここに私が思いついたものがあります:

<?php
$description_string = '<p>Here is the test link: <a href="http://www.youtube.com/watch?v=2MFn8L9tIrg" target="_blank">\'Man or Muppet\'</a> with other text afterwards.</p>';

$description = preg_replace( '/(<a[^>]+youtube[^>]*>)+[^"]*(<\/a>)+/', '$1Watch This Video$2', $description_string );

echo $description;
?>

結果:

<p>Here is the test link: <a href="http://www.youtube.com/watch?v=2MFn8L9tIrg" target="_blank">Watch This Video</a> with other text afterwards.</p>

最大の問題は、引用符 (") がタイトルに含まれていることでした。アンカー タグが切り取られています。$0 の使用も正しくありません。$1 を使用する必要があります。

これはまさにあなたが必要としているものではないかもしれませんが、簡単なモンキー パッチです。

于 2012-07-16T20:25:18.293 に答える