<?php
function convertYoutube($string) {
return preg_replace(
"/\s*[a-zA-Z\/\/:\.]*youtu(be.com\/watch\?v=|.be\/)([a-zA-Z0-9\-_]+)([a-zA-Z0-9\/\*\-\_\?\&\;\%\=\.]*)/i",
"<iframe width=\"420\" height=\"315\" src=\"//www.youtube.com/embed/$2\" allowfullscreen></iframe>",
$string
);
}
$text = "Youtube long url: https://www.youtube.com/watch?v=waIkasvAVGo\n\nYoutube short url: http://youtu.be/waIkasvAVGo";
echo convertYoutube($text);
I found this code on this site:
http://syframework.alwaysdata.net/convert-youtube-url-to-iframe.
The script works just fine. But want this just to work within a BBcode.
For example: [YouTube]<url>[/YouTube].
Does anyone have suggestions to how this can be resolved?
Try this:
<?php
function convertYoutube($string) {
return preg_replace(
"/\[youtube\]\s*[a-zA-Z\/\/:\.]*youtu(be.com\/watch\?v=|.be\/)([a-zA-Z0-9\-_]+)([a-zA-Z0-9\/\*\-\_\?\&\;\%\=\.]*)\[\/youtube\]/i",
"<iframe width='420' height='315' src='//www.youtube.com/embed/$2' allowfullscreen></iframe>",
$string
);
}
$text = "Youtube long url: [youtube]https://www.youtube.com/watch?v=waIkasvAVGo[/youtube]\n\nYoutube short url: [youtube]http://youtu.be/waIkasvAVGo[/youtube]";
echo convertYoutube($text);
?>
Related
My page source have this html (js):
<script>
playlist.sources.push({
label: "480p",
source: "//zingtv-video-14.zadn.vn/Video480/2016/0418/7c/1f1fd73f7432d7524cb43b57da35d6df.mp4?authen=exp=1559094603~acl=1f1fd73f7432d7524cb43b57da35d6df~hmac=01da84812b2ef7d43e55be79fa3ef56e",
index: 1
});</script>
I want to get:
//zingtv-video-14.zadn.vn/Video480/2016/0418/7c/1f1fd73f7432d7524cb43b57da35d6df.mp4?authen=exp=1559094603~acl=1f1fd73f7432d7524cb43b57da35d6df~hmac=01da84812b2ef7d43e55be79fa3ef56e
My code :
<?php
// Check if the URL parameter for our proxy is set.
if (!empty($_GET['url'])) {
if (filter_var($_GET['url'], FILTER_VALIDATE_URL)) {
$grabs = file_get_contents("{$_GET['url']}");
$grab = json_decode($grabs, true);
// I don't know what to do next
echo "{$video}" />";
} else {
echo "Given URL is not valid.";
}
} else {
echo "You need to specify the URL.";
}
?>
and show in the content page.
How do I solve this problem?
Here, we can try using preg_match_all with a simple expression:
source: "(.+)",
where our desired output is saved in a capturing group $1.
Test
$re = '/source: "(.+)",/m';
$str = '<script>
playlist.sources.push({
label: "480p",
source: "//zingtv-video-14.zadn.vn/Video480/2016/0418/7c/1f1fd73f7432d7524cb43b57da35d6df.mp4?authen=exp=1559094603~acl=1f1fd73f7432d7524cb43b57da35d6df~hmac=01da84812b2ef7d43e55be79fa3ef56e",
index: 1
});</script>';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
var_dump($matches[0][1]);
DEMO
I'm not quite sure if this file_get_content would work:
$str = file_get_contents('https://tv.zing.vn/video/id/IWZBFE8U.html?t=388');
$str = mb_convert_encoding($str, 'HTML-ENTITIES', "UTF-8");
var_dump($str);
So, I am using this to replace BBCode to HTML:
$text = htmlspecialchars($text);
$advanced_bbcode = array(
'#\[quote](\r\n)?(.+?)\[/quote]#si',
'#\[url](.+)\[/url]#Usi');
$advanced_html = array(
'<blockquote class="quote">$2</blockquote>',
'<a rel="nofollow" target="_blank" href="$1">$1</a>');
$text = preg_replace($advanced_bbcode, $advanced_html,$text);
echo nl2br($text);
public static function nl2br($var)
{
return str_replace(array('\\r\\n','\r\\n','r\\n','\r\n', '\n', '\r'), '<br />', nl2br($var));
}
This works fine if I only have 1 quote, but If I use multiple quotes like: [quote][quote][quote]first[/quote]second[/quote]end[/quote]
I expect to get:
<blockquote class="quote"><blockquote class="quote"><blockquote class="quote">first</blockquote>second</blockquote>end</blockquote>
But because it takes the first [/qoute] it will turn into:
<blockquote class="quote">[quote][quote]first</blockquote>second[/quote]end[/quote]
I've looked it up but I cant find anything that is working for me. I am new to this kind of stuff.
Thanks.
Make replace until there is BBCode in the string
do {
$text = preg_replace($advanced_bbcode, $advanced_html,$text,-1,$c);
} while($c);
demo
I'm trying to turn youtube links into embed iframes, to play the videos. However, my current code is replacing the entire sentence with the embed code. What I want to do is to just convert the youtube link to an embed code, and leave the rest of the text unharmed.
Example: This is a youtube link: https://www.youtube.com/watch?v=T-8XurAKMkU and some text after.
Turned into: This is a youtube link: <embed> and some text after.
My current code:
$testing = "This is a youtube link: https://www.youtube.com/watch?v=T-8XurAKMkU and some text after.";
echo $core->convertyoutube($testing);
And the function:
public function convertyoutube($link) {
if (strpos($link, 'youtube.com/watch?v=') == true) {
$url = $link;
parse_str(parse_url($url, PHP_URL_QUERY), $youtube_array);
$videoid = $youtube_array['v'];
$embed = "<iframe width='420' height='315' src='https://www.youtube.com/embed/".$videoid."'></iframe>"; // what it should create with the extracted code
return $embed;
}
}
Well You are returning only embed code:
return $embed;
You need to replace only youtube part:
public function convertyoutube($link) {
$position = strpos($link, 'youtube.com/watch?v=');
if ($position !== false) {
$chunks = explode(' ', $link);
foreach ($chunks as &$chunk) {
$isYoutubeLink = strpos($chunk, 'youtube.com/watch?v=');
if ($isYoutubeLink !== false) {
$url = $chunk;
parse_str(parse_url($url, PHP_URL_QUERY), $youtube_array);
$videoid = $youtube_array['v'];
$chunk = "<iframe width='420' height='315' src='https://www.youtube.com/embed/".$videoid."'></iframe>"; // what it should create with the extracted code
}
}
return implode(' ', $chunks);
}
}
It works with multiple links in sentence. I guess there is "better" way with using regexp, however I am not very good at regexp and don't like to use it where it is not mandatory.
You could actually do this all with a single regex.
echo preg_replace('/https?:\/\/(?:www\.)?youtube\.com\/watch\?v=(.+?)(?:&|\s|$)/',
'<iframe width="420" height="315" src="https://www.youtube.com/embed/$1"></iframe>',
'This is a youtube link: https://www.youtube.com/watch?v=T-8XurAKMkU and some text after.');
Output:
This is a youtube link: https://www.youtube.com/watch?v=T-8XurAKMkU and some text after.
Regex101 Demo: https://regex101.com/r/cT2mW1/2
It will be better if you convert the links at the front-end view with javascript to avoid a excess loading of server. But it's your choise.
Single youtube video has different links like these:
1) https://www.youtube.com/watch?v=lfKON5AMvTM
2) https://youtu.be/lfKON5AMvTM
For this reason you should write your codes like this to catch every type of youtube links:
public function convertYouTube($content) {
$content = preg_replace("/http(s)?:\/\/youtu\.be\/([^\40\t\r\n\<]+)/i", '<iframe width="420" height="315" src="https://www.youtube.com/embed/$2"></iframe>', $content);
$content = preg_replace("/http(s)?:\/\/(w{3}\.)?youtube\.com\/watch\/?\?v=([^\40\t\r\n\<]+)/i", '<iframe width="420" height="315" src="https://www.youtube.com/embed/$3"></iframe>', $content);
return $content;
}
$message contains two different Youtube videos. The code below works but the problem is that the end result produces two iframes with the same video ID (the first video). How can I solve this problem?
$message = 'This is a text with 2 Youtube videos: https://www.youtube.com/watch?v=rxwMjB-Skao csassasas http://www.youtube.com/watch?v=VWEwWECAokU Enf of text';
$reg_exUrl_youtube = "/(?:http(?:s)?:\/\/)?(?:www\.)?(?:youtu\.be\/|youtube\.com\/(?:(?:watch)?\?(?:.*&)?v(?:i)?=|(?:embed|v|vi|user)\/))([^\?&\"'> \r\n]+)(?![^<]*>)/";
if (preg_match($reg_exUrl_youtube, $message, $youtubeUrlData) ) {
$message = preg_replace($reg_exUrl_youtube, "<iframe title=\"{$youtubeUrlData[1]}\" class=\"youtube\" src=\"[qqqqq].youtube.com/embed/{$youtubeUrlData[1]}\" frameborder=\"0\" allowFullScreen></iframe>", $message);
}
1) The fix for your code is to use $1 instead of {$youtubeUrlData[1]} in the preg_replace() call:
$message = preg_replace($reg_exUrl_youtube, "<iframe title=\"$1\" class=\"youtube\" src=\"[qqqqq].youtube.com/embed/$1\" frameborder=\"0\" allowFullScreen></iframe>", $message);
2) Another implementation with preg_replace_callback(), which is very reliable from my experience, just as an example:
$message = 'This is a text with 2 Youtube videos: https://www.youtube.com/watch?v=rxwMjB-Skao csassasas http://www.youtube.com/watch?v=VWEwWECAokU Enf of text';
$reg_exUrl_youtube = "/(?:http(?:s)?:\/\/)?(?:www\.)?(?:youtu\.be\/|youtube\.com\/(?:(?:watch)?\?(?:.*&)?v(?:i)?=|(?:embed|v|vi|user)\/))([^\?&\"'> \r\n]+)(?![^<]*>)/";
$finalString = preg_replace_callback($reg_exUrl_youtube, function($matches) {
return "<iframe title=\"{$matches[1]}\" class=\"youtube\" src=\"[qqqqq].youtube.com/embed/{$matches[1]}\" frameborder=\"0\" allowFullScreen></iframe>";
}, $message);
echo htmlentities($finalString);
Why use an error prone regex?
Much simpler:
$message = 'This is a text with 2 Youtube videos: https://www.youtube.com/watch?v=rxwMjB-Skao csassasas http://www.youtube.com/watch?v=VWEwWECAokU Enf of text';
$a=explode(' ',$message);
foreach($a as $v){
if(strpos($v,'http')!==false && strpos($v,'youtube')!==false){
$res[]=$v;
}
}
echo var_dump($res);
Hey I am trying to do 2 preg_replace:
1.make all urls to html links
2.make all images url to html img tag
But these regexs cancel the other one
<?php
// The Regular Expression filter
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
$reg_exImg = "!http://[a-z0-9\-\.\/]+\.(?:jpe?g|png|gif)!Ui";
// The Text you want to filter for urls
$text = "The text you want to filter goes here. http://google.comhttp://www.ynet.co.il http://dogsm.files.wordpress.com/2011/12/d7a1d7a7d795d791d799-d793d795.jpg";
// Check if there is a url in the text
$text = preg_replace($reg_exImg, '<img src=$0 >', $text);
$text = preg_replace($reg_exUrl, '$0', $text);
echo $text;
?>
How can I make that the preg_replace that make url to links dont do this to the tag?
Thanks.
Haim
This might be better as a callback.
Use just the $reg_exUrl, and do this:
$text = preg_replace_callback($reg_exUrl,function($m) {
if( preg_match("/\.(?:jpe?g|png|gif)$/i",$m[0])) {
return "<img src='".$m[0]."' />";
}
else {
return "<a href='".$m[0]."' rel='nofollow'>".$m[0]."</a>";
}
},$text);