Problems with strings and replacement

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pspcrazy
Forum Newbie
Posts: 3
Joined: Thu Jun 12, 2008 8:36 pm

Problems with strings and replacement

Post 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');
 
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: Problems with strings and replacement

Post 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.
pspcrazy
Forum Newbie
Posts: 3
Joined: Thu Jun 12, 2008 8:36 pm

Re: Problems with strings and replacement

Post 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 :|
pspcrazy
Forum Newbie
Posts: 3
Joined: Thu Jun 12, 2008 8:36 pm

Re: Problems with strings and replacement

Post 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.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: Problems with strings and replacement

Post 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.
Post Reply