Hello. Let's say I have a block of text in $content. How do I replace all occurrences of: "http://megavideo.com/?v=[random alphanumeric string here, 8 chars long]" PHP: with something like: "http://megavideo.com/v/[the same random variable taken after the = sign above]" PHP: ...while keeping everything else intact (the rest of the text)?
str_replace or preg_replace would work. For the preg replace use $string = preg_replace('/\?v=/i', 'v/', $string); PHP:
No, no. You got it wrong. What I really want to do is replace all plain Megavideo URLs like: http://www.megavideo.com/?d=AROTWUTX Code (markup): to the embed code like: <object width="450" height="330"> <param name="movie" value="http://wwwstatic.megavideo.com/mv_player.swf?image=&v=4Q7V1T88"></param> <param name="allowFullScreen" value="true"></param> <embed src="http://wwwstatic.megavideo.com/mv_player.swf?image=&v=4Q7V1T88" type="application/x-shockwave-flash" allowfullscreen="true" width="450" height="330"></embed> </object> Code (markup):
Well why show that example to begin with. It has nothing to do with what you want it doing other than it's a mega video link you need to use a regex to capture the video id and output the video html then I guess using regex
Yes, that's what I'm trying to do but I don't know how. The thing is that if I use preg match replacement it will just replace the part after "=" instead of the whole URL which looks like http://megavideo.com/?v=DSAKDSKD As for the example, that was the first video that got in my hands so I gave it as an example. What's wrong with that?
The first result example you gave was completely different from the one you really wanted, that's what was wrong with it. I get the feeling you're still not telling us everything. If you want something that works, you have to give complete examples of input and output.
Ok, here's a complete example. The quote below is assigned to a variable called $string. After doing some matching and replacing I want to receive the content below inside $string: I just love watching videos at Megavideo. Here's one of my favorites: <object width="450" height="330"><param name="movie" value="http://wwwstatic.megavideo.com/mv_player.swf?v=1UGLO8JZ"></param><param name="allowFullScreen" value="true"></param><embed src="http://wwwstatic.megavideo.com/mv_player.swf?v=1UGLO8JZ" type="application/x-shockwave-flash" allowfullscreen="true" width="450" height="330"></embed></object> Also, yesterday I watched another wonderful video there but it doesn't work today: <object width="450" height="330"><param name="movie" value="http://wwwstatic.megavideo.com/mv_player.swf?v=7K3VWSSY"></param><param name="allowFullScreen" value="true"></param><embed src="http://wwwstatic.megavideo.com/mv_player.swf?v=7K3VWSSY" type="application/x-shockwave-flash" allowfullscreen="true" width="450" height="330"></embed></object> HTML:
Untested, but try: <?php $string = <<<EOF I just love watching videos at Megavideo. Here's one of my favorites: http://www.megavideo.com/?v=1UGLO8JZ Also, yesterday I watched another wonderful video there but it doesn't work today: http://www.megavideo.com/?v=7K3VWSSY EOF; function megaupload_embed($match){ return "<object width=\"450\" height=\"330\"><param name=\"movie\" value=\"http://wwwstatic.megavideo.com/mv_player.swf?v=".$match[1]."\"></param><param name=\"allowFullScreen\" value=\"true\"></param><embed src=\"http://wwwstatic.megavideo.com/mv_player.swf?v=".$match[1]."\" type=\"application/x-shockwave-flash\" allowfullscreen=\"true\" width=\"450\" height=\"330\"></embed></object>"; } echo preg_replace_callback("/http:\/\/www\.megavideo\.com\/\?v=([0-9A-Z]*)/", "megaupload_embed", $string); ?> PHP: