我需要什么
我需要11位数的代码,这些代码应该附加在你的管道网址中。
like www.youtube.com/embed/XzqagJTsNrc.
源代码
$url= htmlentities($this->linkify($row['tweetText']));
$youtube=explode(' ',$url).
数组输出。
Array
(
[0] => VW
[1] => @
[2] => European
[3] => Motor
[4] => Show
[5] => Brussels
[6] => -
[7] => Behind
[8] => the
[9] => scene
[10] => (part
[11] => 1):
[12] => &nbsp;<a
[13] => class="twtlnk"
[14] => rel="nofollow"
[15] => href="http://youtu.be/XzqagJTsNrc"
[16] => target=\"_blank\">http://youtu.be/XzqagJTsNrc</a>
[17] => via
[18] => &nbsp;<a
[19] => class="twthand"
[20] => href="https://twitter.com/youtube"
[21] => rel="nofollow"
[22] => target=\"_blank\"
[23] => >@youtube</a>
)
我尝试过使用正则表达式
$text = preg_replace('~
https?:// # Required scheme. Either http or https.
(?:[0-9A-Z-]+\.)? # Optional subdomain.
(?: # Group host alternatives.
youtu\.be/ # Either youtu.be,
| youtube # or youtube.com or
(?:-nocookie)? # youtube-nocookie.com
\.com # followed by
\S* # Allow anything up to VIDEO_ID,
[^\w\s-] # but char before ID is non-ID char.
) # End host alternatives.
([\w-]{11}) # $1: VIDEO_ID is exactly 11 chars.
(?=[^\w-]|$) # Assert next char is non-ID or EOS.
(?! # Assert URL is not pre-linked.
[?=&+%\w.-]* # Allow URL (query) remainder.
(?: # Group pre-linked alternatives.
[\'"][^<>]*> # Either inside a start tag,
| </a> # or inside <a> element text contents.
) # End recognized pre-linked alts.
) # End negative lookahead assertion.
[?=&+%\w.-]* # Consume any URL (query) remainder.
~ix',
'<a href="http://www.youtube.com/watch?v=$1">$1</a>',
$youtube);
print_r($text);
数组返回
Array
(
[0] => VW
[1] => @
[2] => European
[3] => Motor
[4] => Show
[5] => Brussels
[6] => -
[7] => Behind
[8] => the
[9] => scene
[10] => (part
[11] => 1):
[12] => &nbsp;<a
[13] => class="twtlnk"
[14] => rel="nofollow"
[15] => href="<a href="http://www.youtube.com/watch?v=XzqagJTsNrc">XzqagJTsNrc</a>;
[16] => target=\"_blank\"><a href="http://www.youtube.com/watch?v=XzqagJTsNrc">XzqagJTsNrc</a>;/a>
[17] => via
[18] => &nbsp;<a
[19] => class="twthand"
[20] => href="https://twitter.com/youtube"
[21] => rel="nofollow"
[22] => target=\"_blank\"
[23] => >@youtube</a>
)
但我只需要XzqagJTsNrc(11位数字)。
how to parse href="XzqagJTsNrc; (href =" ;)
答案 0 :(得分:1)
您可以使用preg_match
$url = $text[15];
if (preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $url, $match)) {
$video_id = $match[1];
}
echo( $video_id);
它不仅会匹配youtube,还会匹配其他ID
答案 1 :(得分:0)
简单:
$var = 'href="XzqagJTsNrc;';
$youtube_id = substr($var, 6, 11);
答案 2 :(得分:0)
如果您只需要XzqagJTsNrc
,请使用basename
:
$link = 'http://www.youtube.com/embed/XzqagJTsNrc';
echo basename($link); // XzqagJTsNrc