我有一个人们发布更新的输入区域。所以我想过滤 youtube 链接,修改它们并在最后附加它们。
该内容不是html,甚至没有<br>
or <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;
}
它的作用是,它需要 textarea,而不是 #hash,它会替换为<a>#hash</a>
您在社交媒体中看到的主题标签。
我希望我的功能做的是获取 youtube 链接并将其转换为<a>ID</a>
(基本上)
如果我只有 youtube 链接,它可以正常工作,但是当它在它之后或之前带有字符串时,它就会变得疯狂。
我想它不起作用,因为我没有想出第二个 $pattern。在其他程序中有。