如何取代字符串,让youtube在新的共享代码编写


How to replace string to let youtube be written in new share code?

不是程序员,只是一个论坛管理员,所以任何直接的答案都会非常感激,因为我知道这是一个简单的答案。

我有一个过时的论坛模型来嵌入youtube视频。Youtube改变了他们的分享链接,我不知道如何更新这个,这样人们就可以发布旧格式(www.youtube.com/watch?=v(location)和新格式,看起来像Youtube .be/(位置)

$passiveVid[] = array (
    'string' => '#'.$urlrx[0].'(?:[a-zA-Z]{1,4}'.)?youtube.com/(?:watch)?'?v=(.{11}?)'.$urlrx[1].'#',
    'replacement' => '$PASSIVEURL = $matches[1]; $PASSIVETITLE = $matches[3]; $PASSIVEEMBED = ''<object width="712" height="400"><param name="movie" value="http://www.youtube.com/v/''.$matches[2].''"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/''.$matches[2].''" type="application/x-shockwave-flash" wmode="transparent" width="712" height="400"></embed></object>''; return "'.$customstring.'";',
    'id' => 1
    );
$passiveVid[] = array (
    'string' => '#'[youtube']([^'[']]+)'[/youtube']#',
    'replacement' => 'return ''<object width="712" height="400"><param name="movie" value="http://www.youtube.com/v/''.$matches[1].''"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/''.$matches[1].''" type="application/x-shockwave-flash" wmode="transparent" width="712" height="400"></embed></object>'';',
    'id' => 1
    );

虽然我不是真正的正则表达式专家,但这应该可以工作:

$passiveVid[] = array(
    'string' => '#' . $urlrx[0] . '(?:[a-zA-Z]{1,4}'.)?youtube.com/(?:watch)?'?v=(.{11}?)' . $urlrx[1] . '#',
    'replacement' => '$PASSIVEURL = $matches[1]; $PASSIVETITLE = $matches[3]; $PASSIVEEMBED = ''<object width="712" height="400"><param name="movie" value="http://www.youtube.com/v/''.$matches[2].''"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/''.$matches[2].''" type="application/x-shockwave-flash" wmode="transparent" width="712" height="400"></embed></object>''; return "' . $customstring . '";',
    'id' => 1
);
$passiveVid[] = array(
    'string' => '#'[youtube']([^'[']]+)'[/youtube']#',
    'replacement' => 'return ''<object width="712" height="400"><param name="movie" value="http://www.youtube.com/v/''.$matches[1].''"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/''.$matches[1].''" type="application/x-shockwave-flash" wmode="transparent" width="712" height="400"></embed></object>'';',
    'id' => 1
);
$passiveVid[] = array(
    'string' => '#' . $urlrx[0] . '(?:[a-zA-Z]{1,4}'.)?youtu.be/(.{11}?)' . $urlrx[1] . '#',
    'replacement' => '$PASSIVEURL = $matches[1]; $PASSIVETITLE = $matches[3]; $PASSIVEEMBED = ''<object width="712" height="400"><param name="movie" value="http://www.youtube.com/v/''.$matches[2].''"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/''.$matches[2].''" type="application/x-shockwave-flash" wmode="transparent" width="712" height="400"></embed></object>''; return "' . $customstring . '";',
    'id' => 1
);
$passiveVid[] = array(
    'string' => '#'[youtu']([^'[']]+)'[/youtu']#',
    'replacement' => 'return ''<object width="712" height="400"><param name="movie" value="http://www.youtube.com/v/''.$matches[1].''"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/''.$matches[1].''" type="application/x-shockwave-flash" wmode="transparent" width="712" height="400"></embed></object>'';',
    'id' => 1
);

否则你也可以这样做:

$newString = str_replace('youtu.be/', 'youtube.com/watch?v=', $originalString);

在其他代码之上,这比使用正则表达式更容易一点:)。