Page 1 of 1

Problems with strings and replacement

Posted: Thu Jun 12, 2008 8:36 pm
by pspcrazy
Hi i've been working on a script to take in URLS of video site's and convert them to embed's for wordpress, and it's been successful for most sites, except myspacetv which contains ? = characters.

Here's my code so far veoh works, myspace doesn't thanks.

Code: Select all

// myspace code
 
define("MYSP_WIDTH", 540);
define("MYSP_HEIGHT", 438);
define("MYSP_REGEXP", "/http:\/\/vids\.myspace\.com\/index\.cfm\?fuseaction=vids\.individual&VideoID=([[:print:]]+)/");
define("MYSP_TARGET", "<object type=\"application/x-shockwave-flash\" style=\"width:".MYSP_WIDTH."px; height:".MYSP_HEIGHT."px;\" data=\"http://lads.myspace.com/videos/vplayer.swf?m=###URL###&type=video\"><param name=\"movie\" value=\"http://lads.myspace.com/videos/vplayer.swf?m=###URL###&type=video\" /></object>");
 
function mysp_plugin_callback($match)
{
        $output = MYSP_TARGET;
        $output = str_replace("###URL###", strip_tags($match[1]), $output);
        return ($output);
}
 
function mysp_plugin($content)
{
        return (preg_replace_callback(MYSP_REGEXP, 'mysp_plugin_callback', $content));
}
 
add_filter('the_content', 'mysp_plugin');
add_filter('comment_text', 'mysp_plugin');
 
// Veoh code
 
define("VEOH_WIDTH", 540);
define("VEOH_HEIGHT", 438);
define("VEOH_REGEXP", "/http:\/\/www\.veoh\.com\/videos\/([[:print:]]+)/");
define("VEOH_TARGET", "<object type=\"application/x-shockwave-flash\" style=\"width:".VEOH_WIDTH."px; height:".VEOH_HEIGHT."px;\" data=\"http://www.veoh.com/videodetails2.swf?permalinkId=###URL###&id=11975851&player=videodetailsembedded&videoAutoPlay=0\"><param name=\"movie\" value=\"http://www.veoh.com/videodetails2.swf?permalinkId=###URL###&id=11975851&player=videodetailsembedded&videoAutoPlay=0\" /></object>");
 
function veoh_plugin_callback($match)
{
        $output = VEOH_TARGET;
        $output = str_replace("###URL###", strip_tags($match[1]), $output);
        return ($output);
}
 
function veoh_plugin($content)
{
        return (preg_replace_callback(VEOH_REGEXP, 'veoh_plugin_callback', $content));
}
 
add_filter('the_content', 'veoh_plugin');
add_filter('comment_text', 'veoh_plugin');
 

Re: Problems with strings and replacement

Posted: Thu Jun 12, 2008 9:51 pm
by Ollie Saunders
You probably just want to simplify your regular expressions to match more stuff. Can you rely on your URLs being terminated with something? As long as the HTML is valid URLs should all be inside attributes that end with either " or ' so you can match all characters up until them.

Re: Problems with strings and replacement

Posted: Thu Jun 12, 2008 10:02 pm
by pspcrazy
Well not really, sometimes there are 2-3 url's in a post in a row. It does work though, though mabye i don't get what your trying to tell me :|

Re: Problems with strings and replacement

Posted: Thu Jun 12, 2008 10:20 pm
by pspcrazy
Now the codes:

Code: Select all

// myspace code
 
define("MYSP_WIDTH", 540);
define("MYSP_HEIGHT", 438);
define("MYSP_REGEXP", "/http:\/\/vids\.myspace\.com\/index\.cfm\?fuseaction=vids\.individual&VideoID=([_A-Za-z0-9-]+)/");
define("MYSP_TARGET", "<object type=\"application/x-shockwave-flash\" style=\"width:".MYSP_WIDTH."px; height:".MYSP_HEIGHT."px;\" 
 
data=\"http://lads.myspace.com/videos/vplayer.swf?m=###URL###&type=video\"><param name=\"movie\" value=\"http://lads.myspace.com/videos/vplayer.swf?
 
m=###URL###&type=video\" /></object>");
 
function mysp_plugin_callback($match)
{
        $output = MYSP_TARGET;
        $output = str_replace("###URL###", strip_tags($match[1]), $output);
        return ($output);
}
 
function mysp_plugin($content)
{
        return (preg_replace_callback(MYSP_REGEXP, 'mysp_plugin_callback', $content));
}
 
add_filter('the_content', 'mysp_plugin');
add_filter('comment_text', 'mysp_plugin');
 
// Veoh code
 
define("VEOH_WIDTH", 540);
define("VEOH_HEIGHT", 438);
define("VEOH_REGEXP", "/http:\/\/www\.veoh\.com\/videos\/([_A-Za-z0-9-]+)/");
define("VEOH_TARGET", "<object type=\"application/x-shockwave-flash\" style=\"width:".VEOH_WIDTH."px; height:".VEOH_HEIGHT."px;\" 
 
data=\"http://www.veoh.com/videodetails2.swf?permalinkId=###URL###&id=11975851&player=videodetailsembedded&videoAutoPlay=0\"><param name=\"movie\" 
 
value=\"http://www.veoh.com/videodetails2.swf?permalinkId=###URL###&id=11975851&player=videodetailsembedded&videoAutoPlay=0\" /></object>");
 
function veoh_plugin_callback($match)
{
        $output = VEOH_TARGET;
        $output = str_replace("###URL###", strip_tags($match[1]), $output);
        return ($output);
}
 
function veoh_plugin($content)
{
        return (preg_replace_callback(VEOH_REGEXP, 'veoh_plugin_callback', $content));
}
 
add_filter('the_content', 'veoh_plugin');
add_filter('comment_text', 'veoh_plugin');
Still doesn't work oddly.

Re: Problems with strings and replacement

Posted: Thu Jun 12, 2008 10:37 pm
by Ollie Saunders
OK so roughly what I'm saying is you perhaps want to try an adapted version of this:

Code: Select all

$data = '<a href="http://somelink.com">foo</a><a href="http://another.com">foo</a><a href="http://a_third.com">bar</a>';
preg_match_all('~("|\')(http://.+?)\1~i', $data, $matches);
print_r($matches[2]);
What the balls is going on there? There should be escape in front of the single quote in the regex but it doesn't appear on the board. You'll have to quote my post to copy it correctly.