Hello how can I delete anything of this string and have only the XBH1dcHoL6Y ?
<param name="movie" value="http://www.youtube.com/v/XBH1dcHoL6Y&rel=0&hl=en_US&feature=player_embedded&version=3">
My purpose is to have the url like this
http://www.youtube.com/watch?v=XBH1dcHoL6Y
and this is what I found so far (but i can't delete the parameters)
$url = "http://www.youtube.com/v/6n8PGnc_cV4";
$start = strpos($url,"v=");
echo 'http://www.youtube.com/v/'.substr($url,$start+2);
Thank you!
Obligatory one-liner:
echo end(explode('/', reset(explode('&', 'http://www.youtube.com/v/XBH1dcHoL6Y&rel=0&hl=en_US&feature=player_embedded&version=3'))));
Edit: preg_match version:
$string = '<embed src="http://www.youtube.com/v/XBH1dcHoL6Y&rel=0&hl=en_US&feature=player_embedded&-version=3" type="application/x-shockwave-flash" allowfullscreen="true" allowScriptAccess="always" width="640" height="390"></embed>';
$expr = "/<embed.*https?:\/\/www.youtube.com\/v\/([a-zA-Z0-9]+).*<\/embed>/";
if(preg_match($expr, $string, $matches))
echo 'Matched: '.$matches[1];
else
echo 'No match';
// returns "Matched: XBH1dcHoL6Y"
try this:
$url = "http://www.youtube.com/v/XBH1dcHoL6Y&rel=0&hl=en_US";
$urlSplode = explode('/',$url);
$urlEnd = $urlSplode[count($urlSplode)-1];
$urlEndSplode = explode('&',$urlEnd);
$finalStr = $urlEndSplode[0];
echo "http://www.youtube.com/watch?v=$finalStr";
here is the demo:
http://codepad.org/aoGLZSX0
$urlTokens = explode("/", "http://www.youtube.com/v/XBH1dcHoL6Y&rel=0&hl=en_US&feature=player_embedded&version=3");
$urlTokens = explode("&", $urlTokens[4]);
$url = $urlTokens[0]; // will output XBH1dcHoL6Y
Related
I have a link like below:
$link = http://mytour.com:8080/en/admin/dashboard//tmp/Rc4Lw3
How can i get it to be like the one below in PHP:
$link = http://mytour.com:8080
Sorry if this is a newbie question. Thanks.
parse_url($link)
$link = 'http://mytour.com:8080/en/admin/dashboard//tmp/Rc4Lw3';
$parsed_link = parse_url($link);
echo $parsed_link['scheme']."//".$parsed_link['host'].":".$parsed_link['port'];
preg_match() - worse
preg_match("/^http[s]*:\/\/.*:\d+/", $link, $match);
echo $match[0];
You can use parse_url() to get url you want
$url = 'http://mytour.com:8080/en/admin/dashboard//tmp/Rc4Lw3';
$parse_url = parse_url($url);
echo $link = $parse_url['scheme']."://".$parse_url['host'].":".$parse_url['port'];
echo "<br>";
//You can also use default method and params
echo $link = parse_url($url, PHP_URL_SCHEME)."//".parse_url($url, PHP_URL_HOST).":".parse_url($url, PHP_URL_PORT);
As suggested by #vladkras use parse_url($link)
$url = 'http://mytour.com:8080/en/admin/dashboard//tmp/Rc4Lw3';
$parse = parse_url($url);
echo $parse['scheme'].'://'.$parse['host'].':'.$parse['port'];
Output:
http://mytour.com:8080
$link = 'http://mytour.com:8080/en/admin/dashboard//tmp/Rc4Lw3';
$url = parse_url($link);
echo $url['scheme'].'://'.$url['host'].':'.$url['port'];
I currently have a code that finds and replaces urls into complete html links. It works fine but now i need to update it so that if there is image url then it should convert it into a html img tag and display it. Function im using now is...
function auto_link_text($text) {
$pattern = '#\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))#';
$callback = create_function('$matches', '
$url = array_shift($matches);
$url_parts = parse_url($url);
return sprintf(\'<a rel="nowfollow" target="_blank" href="%s">%s</a>\', $url, $url);
');
return preg_replace_callback($pattern, $callback, $text);
}
Got it from...
How to add anchor tag to a URL from text input
Here is an example of the text i would to it to go through...
asdf
http://google.com/
asfd
http://yahoo.com/logo.jpg
http://www.apple.com/sdfsd.php?page_id=13&id=18210&status=active#1
http://youtube.com/logo.png
like it updated function to output...
asdf
<a rel="nowfollow" target="_blank" href="http://google.com/">http://google.com/</a>
asfd
<img src="http://yahoo.com/logo.jpg" class="example">
<a rel="nowfollow" target="_blank" href="http://www.apple.com/sdfsd.php?page_id=13&id=18210&status=active#1">http://www.apple.com/sdfsd.php?page_id=13&id=18210&status=active#1</a>
<img src="http://youtube.com/logo.png" class="example">
Big thanks in advance!
You can use this for example:
function create_anchor_tag($url, $text = false) {
if ($text===false) $text = $url;
return '<a rel="no-follow" target="_blank" href="' . $url . '">'
. $text . '</a>';
}
function create_image_tag($url) {
return '<img src="' . $url . '"/>';
}
function auto_link_text($text) {
$pattern = '~\b(?:(?:ht|f)tps?://|www\.)\S+(?<=[\PP?])~i';
$callback = function ($m) {
$img_ext = array('jpg', 'jpeg', 'gif', 'png');
$path = parse_url($m[0], PHP_URL_PATH);
$ext = substr(strrchr($path, '.'), 1);
if (in_array(strtolower($ext), $img_ext))
return create_image_tag($m[0]);
return create_anchor_tag($m[0]);
};
return preg_replace_callback($pattern, $callback, $text);
}
I used several functions to make it more clea[rn], but you can easily adapt it as you like.
Here is the nice post about the best suitable regex pattern for valid URL. I picked one from there to group all the URLs.
Online demo
Steps to follow:
simply extract the url.
put a check on the url and based on your own logic substitute the tag as shown in demo.
sample code: (get all the valid urls in groups. get it from index 1)
$re = "/(([A-Za-z]{3,9}:(?:\\/\\/)?(?:[-;:&=\\+\\$,\\w]+#)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\\+\\$,\\w]+#)[A-Za-z0-9.-]+)((?:\\/[\\+~%\\/.\\w-_]*)?\\??(?:[-\\+=&;%#.\\w_]*)#?(?:[\\w]*))?)/";
$str = "...";
preg_match_all($re, $str, $matches);
sample code: (substitute anchor tag (or what ever you want to add))
$re = "/(([A-Za-z]{3,9}:(?:\\/\\/)?(?:[-;:&=\\+\\$,\\w]+#)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\\+\\$,\\w]+#)[A-Za-z0-9.-]+)((?:\\/[\\+~%\\/.\\w-_]*)?\\??(?:[-\\+=&;%#.\\w_]*)#?(?:[\\w]*))?)/";
$str = "...";
$subst = '$1';
$result = preg_replace($re, $subst, $str);
Hello I'm trying to convert youtube links into embed code.
this is what I have:
<?php
$text = $post->text;
$search = '#<a(.*?)(?:href="https?://)?(?:www\.)?(?:youtu\.be/|youtube\.com(?:/embed/|/v/|/watch?.*?v=))([\w\-]{10,12}).*$#x';
$replace = '<center><iframe width="560" height="315" src="http://www.youtube.com/embed/$2" frameborder="0" allowfullscreen></iframe></center>';
$text = preg_replace($search, $replace, $text);
echo $text;
?>
It works for one link. However if I add two, it will only swap the last occurrence. What do I have to change?
You're not handling the end of the string properly. Remove the $, and replace it with the closing tag </a>. this will fix it.
$search = '#<a(.*?)(?:href="https?://)?(?:www\.)?(?:youtu\.be/|youtube\.com(?:/embed/|/v/|/watch?.*?v=))([\w\-]{10,12}).*<\/a>#x';
$replace = '<center><iframe width="560" height="315" src="http://www.youtube.com/embed/$2" frameborder="0" allowfullscreen></iframe></center>';
$text = preg_replace($search, $replace, $text);
Try this : preg_replace($search, $replace, $text, -1);
I know it's the default but who knows...
EDIT Try that if not working ;
do{
$text = preg_replace($search, $replace, $text, -1, $Count);
}
while($Count);
Here are a regular expresion more efficient: http://pregcopy.com/exp/26, tralate this to PHP: (add "s" modifier)
<?php
$text = $post->text;
$search = '#<a (?:.*?)href=["\\\']http[s]?:\/\/(?:[^\.]+\.)*youtube\.com\/(?:v\/|watch\?(?:.*?\&)?v=|embed\/)([\w\-\_]+)["\\\']#ixs';
$replace = '<center><iframe width="560" height="315" src="http://www.youtube.com/embed/$1" frameborder="0" allowfullscreen></iframe></center>';
$text = preg_replace($search, $replace, $text);
echo $text;
?>
Test it
There are two types of youtube link for one video:
Example:
$link1 = 'https://www.youtube.com/watch?v=NVcpJZJ60Ao';
$link2 = 'https://www.youtu.be/NVcpJZJ60Ao';
This function handles both:
function getYoutubeEmbedUrl($url)
{
$shortUrlRegex = '/youtu.be\/([a-zA-Z0-9_-]+)\??/i';
$longUrlRegex = '/youtube.com\/((?:embed)|(?:watch))((?:\?v\=)|(?:\/))([a-zA-Z0-9_-]+)/i';
if (preg_match($longUrlRegex, $url, $matches)) {
$youtube_id = $matches[count($matches) - 1];
}
if (preg_match($shortUrlRegex, $url, $matches)) {
$youtube_id = $matches[count($matches) - 1];
}
return 'https://www.youtube.com/embed/' . $youtube_id ;
}
The output of $link1 or $link2 would be the same :
$output1 = getYoutubeEmbedUrl($link1);
$output2 = getYoutubeEmbedUrl($link2);
// output for both: https://www.youtube.com/embed/NVcpJZJ60Ao
Now you can use the output in iframe!
Here is my code:
$search = array('<script src="/',
'<link href="/',
'<a href="/',
'<img src="/',
'src="/');
$d = 'http://www.ifreewind.net';
$replace = array('<script src="'.$d.'/',
'<link href="'.$d.'/',
'<a href="'.$d.'/',
'<img src="'.$d.'/',
'src="'.$d.'/');
$result = str_replace($search, $replace, $contents);
echo $result;
These code have a problem is that they cannot replace img tag such as :
<img width="50px" src="/...">
into
<img width="50px" src="http://www.ifreewind.net/...">
How to fix that?
You can't use str_replace for this. You could try it with preg_replace:
preg_replace('~(src|href)="(?=/)~', '$1http://www.ifreewind.net', $contents);
However, I'd strongly advise you to use an HTML parser instead.
Hi I'm developing a custom forum on my website. I would like to convert the urls starting with : http://*.domain.com/photos/{username}/{photo_id} (I should get both username and photo_id ) to the direct image tag so that user get the image instead of the url.
This should be done if they insert the url with or without bbcode:
ie:
http://domain.com/photos/musthafa/12345
[url=http://domain.com/photos/musthafa/12345]my photo link here[/url]
[url=http://domain.com/photos/musthafa/12345]http://domain.com/photos/musthafa/12345[/url]
This should be converted to < html-imge tag src="url-to_photo-path/photo_id.j_p_g" />
I tried this:
$str = "http://www.domain.com/photos/musthafa/12345"
$str = preg_replace_callback("'\[url=http:\/\/www\.domain\.com\/photos\/(.*?)\](.*?)\[/url\]'i", 'self::parse_photo_url', $str);
AND
$str = preg_replace_callback("#^https?://([a-z0-9-]+\.)domain.com/photos/(.*?)$#", 'self::parse_gpp_photo', $str);
function parse_photo_url($url){
{
$full_url = "http://www.domain.com/" . $url[1];
$url_segs = parse_url($full_url);
$path = explode("/", $url_segs['path']);
return '<img src="http://www.domain.com/{path-to-the-gallery}/'.$path[2].'/jpg" />';
}
Musthafa.
I'm not sure that I've exactly got what you want but please try this:
<?php
$image_url_samples = array(
"http://domain.com/photos/musthafa/12345",
"[url=http://domain.com/photos/musthafa/12345]my photo link here[/url]",
"[url=http://domain.com/photos/musthafa/12345]http://domain.com/photos/musthafa/12345[/url]"
);
foreach ($image_url_samples as $image_url_sample)
{
$conversion_result = preg_replace("/^(http:\\/\\/domain.com\\/photos\\/\\w+\\/\\d+)$/i",
"<img src=\"\\1.jpg\" alt=\"\\1\" />", $image_url_sample);
$conversion_result = preg_replace("/^\\[url=(http:\\/\\/domain.com\\/photos\\/\\w+\\/\\d+)\\](.+)\\[\\/url\\]$/",
"<img src=\"\\1.jpg\" alt=\"\\2\" />", $conversion_result);
print $conversion_result . "<br />";
}
The output is:
<img src="http://domain.com/photos/musthafa/12345.jpg" alt="http://domain.com/photos/musthafa/12345" />
<br />
<img src="http://domain.com/photos/musthafa/12345.jpg" alt="my photo link here" />
<br />
<img src="http://domain.com/photos/musthafa/12345.jpg" alt="http://domain.com/photos/musthafa/12345" />
<br />
By the way, if you want to make your URL case insensitive add the i modifier to the end of regular pattern (PHP Pattern Modifiers).
Basically I would like to extract the id (12345 in this example) and I need to extract the direct link to the image url so that, user should get the image tag instead of url.
Thats why I called
preg_replace_callback function.
In simple words, I'm stuck at the regex pattern to match my domain url starting with:
http://domain.com/photos{username}/{id}
OR
http://www.domain.com/photos{username}/{id}"
You don't need regex.
function buildLink( $id, $name ){
$html = array();
$html[] = '<img alt="" src="http://www.domain.com/photos/';
$html[] = $name;
$html[] = '/';
$html[] = $id;
$html[] = '.jpg">';
return implode( '', $html );
}
$path = 'http://www.domain.com/photos/musthafa/12345';
$path = rtrim( $path, ' /' );
$path_parts = explode( '/', $path );
$id = ( int ) array_pop( $path_parts );
$name = array_pop( $path_parts );
$img = buildLink( $id, $name );
echo $img;