This is my function:
function bbc2html($content) {
$search = array (
'/(\[b\])(.*?)(\[\/b\])/',
'/(\[u\])(.*?)(\[\/u\])/',
'/(\[i\])(.*?)(\[\/i\])/',
'/(\[youtube\])(.*?)(\[\/youtube\])/'
);
$replace = array (
'<b>$2</b>',
'<u>$2</u>',
'<i>$2</i>',
'<div class="video_wrapper card border-0 shadow text-center m-2" style="width: 25rem;">
<div class="video_trigger card-body" data-source="'.getYoutubeVideoID('$2').'" data-type="youtube">
<p class="text-center">As soon as you click on the button you accept that cookies from YouTube to be loaded</p>
<input type="button" class="btn btn-success" value="OK">
<a href="$2" type="button" target="_blank" class="btn btn-danger"><i class="fa-brands fa-youtube pe-1"></i>direct link</a>
</div>
<div class="video_layer" style="display: none;">
<iframe src="" border="0" data-scaling="true" data-format="16:9" style="height: 0px;"></iframe>
</div>
</div>'
);
return preg_replace($search, $replace , $content);
}
function getYoutubeVideoID($link):string{
$baseUrl = parse_url($link, PHP_URL_HOST);
if($baseUrl == "youtu.be"){
$urlParts = explode('/',$link);
$id = end($urlParts);
} elseif (preg_match('/(youtube.com)/i',$baseUrl)) {
if(preg_match('/(youtube.com\/watch\?v=)/i',$link)){
$urlParts = explode('?v=',$link);
$id = end($urlParts);
} elseif(preg_match('/(youtube.com\/)/i',$link)) {
$urlParts = explode('/',$link);
$id = end($urlParts);
} else {
return "";
}
} else {
return "";
}
return $id;
}
I want to pass the variable ($2, contains the link to the youtube video) to the function (getYoutubeVideoID, returns only the id of the video), but it just pass '$2' instead of the link.
Example: [youtube] https://www.youtube.com/watch?v=LXb3EKWsInQ [/youtube]
This should be replaced with:
<div class="video_wrapper card border-0 shadow text-center m-2" style="width: 25rem;">
<div class="video_trigger card-body" data-source="LXb3EKWsInQ" data-type="youtube">
<p class="text-center">As soon as you click on the button you accept that cookies from YouTube to be loaded</p>
<input type="button" class="btn btn-success" value="OK">
<a href="https://www.youtube.com/watch?v=LXb3EKWsInQ" type="button" target="_blank" class="btn btn-danger"><i class="fa-brands fa-youtube pe-1"></i>direct link</a>
</div>
<div class="video_layer" style="display: none;">
<iframe src="" border="0" data-scaling="true" data-format="16:9" style="height: 0px;"></iframe>
</div>
</div>