簡単なテキストがあるとしましょう:
テスト テスト テストhttp://www.youtube.com/watch?v=pzfAdmAtYIY その他のテストとランダム テキスト http://www.youtube.com/watch?v=UZQ_RDb0lcEその他のテキストなど
また、単純な配列もあります。
$arr = array('Samsung Mobile USA - El Plato Supreme', 'SET FIRE | DUBSTEP');
これを達成する方法:
テスト テスト テスト pzfAdmAtYIY Samsung Mobile USA - El Plato Supreme より多くのテストとランダム テキスト UZQ_RDb0lcE SET FIRE | ダブステップ詳細テキストなど
私の試み:
$count = 0;
$text = 'testing testing testing http://www.youtube.com/watch?v=pzfAdmAtYIY more testing and random text http://www.youtube.com/watch?v=UZQ_RDb0lcE more text etc';
$arr = array('Samsung Mobile USA - El Plato Supreme', 'SET FIRE | DUBSTEP');
$string = preg_replace('/http:\/\/www.youtube.com\/watch\?v=([a-zA-Z0-9_-]*)/ms', ' \\1 '. $arr[$count++].'', $text);
print $string;
残念ながら結果:
testing testing testing pzfAdmAtYIY Samsung Mobile USA - El Plato Supreme more testing and random text UZQ_RDb0lcE Samsung Mobile USA - El Plato Supreme more text など
どんな助けでも素晴らしいでしょう。