我有一个输入区,人们可以在此发布更新。
因此,我想过滤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;
}
它要做的是,它需要textarea,而不是#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;
}