I have a input area where people post updates. So I want to filter youtube links, modify them and append them in the end.
This content is not html, it even does not have <br>
or <p>
, it's just pure string.
Here is the code I've got from different part of program.
What this should do is, take all matches, and replace them with 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;
}
here is a original code:
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;
}
what it does is, it takes textarea, and instead of #hash it replaces with <a>#hash</a>
hashtags like you see in social media.
what I want my function to do, is to take youtube links and convert it to <a>ID</a>
(basically)
It works fine If I have only youtube link, but when it's with string after or before it, it just goes crazy.
I guess it does not work because I didn't came up with second $pattern. which was there in other program.