Jump to content

Parsing Text


Wolf95

Recommended Posts

there are many options for something like this so i will just throw one out there:

 

$url = preg_replace('|http:\/\/|i', '[url=http://www.youtube.com/watch?v=ty62YzGryU4]http://www.youtube.com/watch?v=ty62YzGryU4[/url]', $url);
if(preg_match('|watch|i', $url))
{
     $url_parts = explode('=', $url);
     $vid_code = $url_parts[1];
}
else
{
     $url_parts = explode('/', $url);
      $vid_code = $url_parts[1];
}

 

first i removed the http from the url so the else statement doesnt throw up too many array keys.

 

if the word watch is in the url then we split the url at the "=" sign which gives an array with 2 keys, the code is the second key.

 

if the word watch isn't in the url then we split the url at the "/" (that's why i removed the http) and the code is the second key in the array

Link to comment
Share on other sites

Here's an embed I made and seems to work all instances.

 

<?php
//parse the host, no http:// returned
function parseHOST($url){
$new_parse_url = str_ireplace(array("www.","mms://","rtsp://","http://","https://", "http://", "ftp://", "feed://"), "", trim($url));
$parsedUrl = @parse_url("http://$new_parse_url");
return strtolower(trim($parsedUrl['host'] ? $parsedUrl['host'] : array_shift(explode('/', $parsedUrl['path'], 2))));
}

function embedYOUTUBE($url){
$parsed_url = parseHOST($url);

//convert youtu.be shortened to normal
if($parsed_url == "youtu.be"){
$vidid = end(explode("/",trim($url)));
$url = "http://www.youtube.com/v/$vidid";
}

//youtube media embed
if(preg_match("/(youtube.com\/(watch?|v\/|v=))/i",$url)){
$url = str_ireplace(array("&autoplay=$vidid","?f=videos","?f=playlists","&app=youtube_gdata","&feature=related"),"",$url);
$vidparser = parse_url($url);
parse_str($vidparser[query], $query);
$vidid = ($query['v']);
if($vidid == ""){
$vidid = end(explode("/",$url));
}

//clean parameters and get title
$vidid = str_ireplace(array("&app=youtube_gdata","&autoplay=$vidid","&autoplay=1","&fs=1"),"",$vidid);
$visit_url = "http://gdata.youtube.com/feeds/api/videos/".$vidid;
$doc = new DOMDocument;
@$doc->load($visit_url);
$link_title = $doc->getElementsByTagName("title")->item(0)->nodeValue;
if($link_title == ""){
$link_title = "Click to watch video.";
}

//show results on page
echo "<div>";
echo "<h2><a href='$url' TARGET='_blank'>$link_title</a></h2>";
echo $vidid."<br />";
echo "<object style='height: 390px; width: 640px'>
<param name='movie' value='http://www.youtube.com/v/$vidid&fs=1'>
<param name='allowFullScreen' value='true'>
<param name='allowScriptAccess' value='always'>
<embed src='http://www.youtube.com/v/$vidid&fs=1' type='application/x-shockwave-flash' allowfullscreen='true' allowScriptAccess='always' width='640' height='390'></object>";
echo "</div>";
} else {
return $url;
}
}

//examples
echo embedYOUTUBE("http://www.youtube.com/watch?v=EHtWe4_Fkok&feature=watch-now-button&wide=1");
echo embedYOUTUBE("http://www.youtube.com/CartooningGenius");
echo embedYOUTUBE("http://www.youtube.com/v/7JxfgId3XTs&fs=1");
echo embedYOUTUBE("http://youtu.be/jlgVLblVSu8");
echo embedYOUTUBE("http://www.youtube.com/watch?v=w5Beh6fEJVs&feature=topvideos_sports");
?>

Link to comment
Share on other sites

slight modification to return the url and not embed the player if not a youtube video link.

 

<?php
//parse the host, no http:// returned
function parseHOST($url){
$new_parse_url = str_ireplace(array("www.","mms://","rtsp://","http://","https://", "http://", "ftp://", "feed://"), "", trim($url));
$parsedUrl = @parse_url("http://$new_parse_url");
return strtolower(trim($parsedUrl['host'] ? $parsedUrl['host'] : array_shift(explode('/', $parsedUrl['path'], 2))));
}

function embedYOUTUBE($url){
$parsed_url = parseHOST($url);

if($parsed_url == "youtu.be" || $parsed_url == "youtube.com"){

//convert youtu.be shortened to normal
if($parsed_url == "youtu.be"){
$vidid = end(explode("/",trim($url)));
$url = "http://www.youtube.com/v/$vidid";
}

//youtube media embed
if(preg_match("/(youtube.com\/(watch?|v\/|v=))/i",$url)){
$url = str_ireplace(array("&autoplay=$vidid","?f=videos","?f=playlists","&app=youtube_gdata","&feature=related"),"",$url);
$vidparser = parse_url($url);
parse_str($vidparser[query], $query);
$vidid = ($query['v']);
if($vidid == ""){
$vidid = end(explode("/",$url));
}

//clean parameters and get title
$vidid = str_ireplace(array("&app=youtube_gdata","&autoplay=$vidid","&autoplay=1","&fs=1"),"",$vidid);
$visit_url = "http://gdata.youtube.com/feeds/api/videos/".$vidid;
$doc = new DOMDocument;
@$doc->load($visit_url);
$link_title = $doc->getElementsByTagName("title")->item(0)->nodeValue;
if($link_title == ""){
$link_title = "Click to watch video.";
}

//show results on page
echo "<div>";
echo "<h2><a href='$url' TARGET='_blank'>$link_title</a></h2>";
echo $vidid."<br />";
echo "<object style='height: 390px; width: 640px'>
<param name='movie' value='http://www.youtube.com/v/$vidid&fs=1'>
<param name='allowFullScreen' value='true'>
<param name='allowScriptAccess' value='always'>
<embed src='http://www.youtube.com/v/$vidid&fs=1' type='application/x-shockwave-flash' allowfullscreen='true' allowScriptAccess='always' width='640' height='390'></object>";
echo "</div>";
} else {
return "<a href='$url'>$url</a>";
}
} else {
return "<a href='$url'>$url</a>";
}
}

//examples
echo embedYOUTUBE("http://phpfreaks.com");
echo embedYOUTUBE("http://www.youtube.com/watch?v=EHtWe4_Fkok&feature=watch-now-button&wide=1");
echo embedYOUTUBE("http://www.youtube.com/CartooningGenius");
echo embedYOUTUBE("http://www.youtube.com/v/7JxfgId3XTs&fs=1");
echo embedYOUTUBE("http://youtu.be/jlgVLblVSu8");
echo embedYOUTUBE("http://www.youtube.com/watch?v=w5Beh6fEJVs&feature=topvideos_sports");
?>

Link to comment
Share on other sites

I redid this a lot, all you need to do is use the $text value as whatever your text value may be.

 

<?php
//your text input from a post
$text = "First lets do a hyperlink: <a href='http://www.youtube.com/watch?v=YaxKiZfQcX8'>Visit my youtube link</a> Some sample text with WWW.AOL.com. <br /> Here's a shortened link - youtu.be/ZzDm2vMHabE <br />http://www.youtube.com/watch?v=csgZ2b1bW2o and a cool user page is http://www.youtube.com/user/cometodoyourwill<br />Anyone use www.myspace.com?  <br />Some people are nuts, look at this stargate link at http://www.youtube.com/watch?v=ZKoUm6z5SzU&feature=grec_index , like aliens exist or something. http://www.youtube.com/watch?v=sfN-7HczmOU&feature=grec_index  and here's a secure site https://familyhistory.hhs.gov, unless you use curl or allow secure connections it will never get a title. <br /> This is a not valid site http://zzzzzzz and this is a dead site http://zwzwzwxzw.com.<br /> Lastly lets try an already made hyperlink and see what it does <a href='http://dynaindex.com'>dynaindex.com</a>";

//space anything that would get included in a link, add the space
$text = str_ireplace(array("<br />","\n","\r"),array(" <br /> "," \n "," \r "),$text);
$text = str_replace("  ", " ", $text);
//explode the text by spaces
$text_explode = explode(" ",$text);
//loop replacing youtube links with embed codes, if not youtube embed return text
foreach($text_explode as $words){
if (preg_match('~(?:youtube\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})~i', $words, $match)) {
$vidid = $match[1];//the id
$link = "http://youtu.be/$vidid";//short link to video

//grab the title
$visit_url = "http://gdata.youtube.com/feeds/api/videos/".$vidid;
$doc = new DOMDocument;
@$doc->load($visit_url);
$link_title = $doc->getElementsByTagName("title")->item(0)->nodeValue;
if($link_title == ""){
$link_title = "Click to watch video.";
}
//display a link
echo "<h3><a href='$link' TARGET='_blank'>$link_title</a></h3>";
//embed the video
echo "<div>";
echo "<object style='height: 390px; width: 640px'>
<param name='movie' value='http://www.youtube.com/v/$vidid&fs=1'>
<param name='allowFullScreen' value='true'>
<param name='allowScriptAccess' value='always'>
<embed src='http://www.youtube.com/v/$vidid&fs=1' type='application/x-shockwave-flash' allowfullscreen='true' allowScriptAccess='always' width='640' height='390'></object>";
echo "</div><br />";
} else {
//keep the original words
echo " $words ";
}
}

?>

 

 

Here's the demo of this code.

http://get.blogdns.com/test/embed-youtube2.php

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.