我有一个输入区域,人们在那里发布更新。所以我想过滤YouTube链接,修改它们并最终附加它们。
这个内容不是html,它甚至没有<br>
或<p>
,它只是纯字符串。
这是我从程序的不同部分获得的代码。
这应该做的是,获取所有匹配项,并用 html 替换它们。
function aKaFilter( $content ) {
global $bp;
$pattern2 = '#^(?:https?://)?(?:www'.)?(?:youtube(?:-nocookie)?'.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu'.be/)([^"&?/ ]{11})(?:.+)?$#x';
preg_match_all( $pattern2, $content, $youtubes );
if ( $youtubes ) {
/* Make sure there's only one instance of each video */
if ( !$youtubes = array_unique( $youtubes[1] ) )
return $content;
//but we need to watch for edits and if something was already wrapped in html link - thus check for space or word boundary prior
foreach( (array)$youtubes as $youtube ) {
$pattern = "NEW". $youtube ."PATTERN TO MATCH THIS LINK";
$content = preg_replace( $pattern, '<span class="video youtube" data-trigger="'.$youtube.'"><img src="http://img.youtube.com/vi/'.$youtube.'/0.jpg"><span class="icon-stack"><i class="icon-circle icon-stack-base"></i><i class="icon-youtube-play"></i></span><span>title</span></span>', $content );
}
}
return $content;
}
这是一个原始代码:
function etivite_bp_activity_hashtags_filter( $content ) {
global $bp;
//what are we doing here? - same at atme mentions
//$pattern = '/[#]([_0-9a-zA-Z-]+)/';
$pattern = '/(?(?<!color: )(?<!color: )[#]([_0-9a-zA-Z-]+)|(^|'s|'b)[#]([_0-9a-zA-Z-]+))/';
preg_match_all( $pattern, $content, $hashtags );
if ( $hashtags ) {
/* Make sure there's only one instance of each tag */
if ( !$hashtags = array_unique( $hashtags[1] ) )
return $content;
//but we need to watch for edits and if something was already wrapped in html link - thus check for space or word boundary prior
foreach( (array)$hashtags as $hashtag ) {
$pattern = "/(^|'s|'b)#". $hashtag ."($|'b)/";
$content = preg_replace( $pattern, ' <a href="' . $bp->root_domain . "/" . $bp->activity->slug . "/". BP_ACTIVITY_HASHTAGS_SLUG ."/" . htmlspecialchars( $hashtag ) . '" rel="nofollow" class="hashtag">#'. htmlspecialchars( $hashtag ) .'</a>', $content );
}
}
return $content;
}
它的作用是,它采用文本区域,而不是 #hash 它替换为<a>#hash</a>
就像您在社交媒体中看到的那样。
我希望我的函数做的是获取YouTube链接并将其转换为<a>ID</a>
(基本上(
如果我只有 youtube 链接,它工作正常,但是当它在它之后或之前带有字符串时,它就会变得疯狂。
我想它不起作用,因为我没有想出其他程序中的第二个$pattern。
为什么需要preg_replace((? str_replace(( 在您的情况下就足够了。此外,您可能需要迭代 $youtubes[0],而不是$youtubes。此外,还可以简化您的代码!;-)
因此,这应该有效:
function aKaFilter( $content ) {
global $bp;
$pattern2 = '#^(?:https?://)?(?:www'.)?(?:youtube(?:-nocookie)?'.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu'.be/)([^"&?/ ]{11})(?:.+)?$#x';
preg_match_all( $pattern2, $content, $youtubes );
/* Make sure there's only one instance of each video */
$youtubes = array_unique( $youtubes[1] );
if ( $youtubes ) {
//but we need to watch for edits and if something was already wrapped in html link - thus check for space or word boundary prior
foreach( $youtubes[0] as $youtube ) {
$content = str_replace( $youtube, '<span class="video youtube" data-trigger="'.$youtube.'"><img src="http://img.youtube.com/vi/'.$youtube.'/0.jpg"><span class="icon-stack"><i class="icon-circle icon-stack-base"></i><i class="icon-youtube-play"></i></span><span>title</span></span>', $content );
}
}
return $content;
}
根本不要使用正则表达式,请使用 parse_url
.
例如:
$parsed_url = parse_url($content);
if (in_array($parsed_url['host'], array('www.youtube.com', 'youtube.com', 'www.youtube-nocookie.com', 'youtube-nocookie.com'))) {
## Now look through $parsed_url['query'] for the video ID
## Parsing this out is a separate question :)
}
尝试使用包含文本的正则表达式匹配 URL 时,问题是您无法知道 URL 何时结束。
URL可以包含"空格"、".
"、",
"和其他字符,因此您不能说 URL 在新单词开始或句子结束时结束。此外,您的正则表达式(?:.+)?
的结尾将匹配(几乎(所有内容。
如果您假设 yutube URL 不能包含空格(在 URL 的给定位置/索引之后(,您可以通过 (?:[^'s]+)?
(除空格外的所有字符(更改正则表达式的末尾,您可以将其他字符添加到集合中以定义 URL 的结尾,例如,如果 URL 也不能包含,
, 你做(?:[^'s,]+)?
,等等。
然后,在正则表达式上设置开始和结束锚点(^
和$
(。当您的 URL 被某些文本包围时,这可能不起作用,因此您可以删除这些锚点并在正则表达式的开头添加'b
(单词边界(锚点。
顺便说一句,您可以用.*
替换(?:.+)?
,用`[^'s,]*
替换(?:[^'s,]+)?
你现在有一个这样的正则表达式:'#'b(?:https?://)?(?:www'.)?(?:youtube(?:-nocookie)?'.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu'.be/)([^"&?/ ]{11})[^'s,]*#x'
铌。我没有分析您的正则表达式的所有逻辑,所以我的评论只对您的正则表达式的开头和结尾有价值。
尝试使用 url :
结果为 JSON 格式。http://gdata.youtube.com/feeds/mobile/videos?alt=json&q=music&format=1,5,6
XML 格式的结果http://gdata.youtube.com/feeds/mobile/videos?q=music&format=1,5,6
然后对于 XML 格式在 -- 标签:youtube.com,2008:视频:qycqF1CWcXg并检索视频ID,即本例中的"qycqF1CWcXg">
适用于 JSON 格式的相同步骤。