Simple PHP Replace All Occurences with Something Else (stristr isn's suitable)

Discussion in 'PHP' started by ColorWP.com, Feb 13, 2010.

  1. #1
    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)?
     
    ColorWP.com, Feb 13, 2010 IP
  2. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #2
    Something like this ?
    str_replace('?v=', 'v/', $string);
    PHP:
     
    joebert, Feb 13, 2010 IP
  3. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #3
    str_replace or preg_replace would work. For the preg replace use

    $string = preg_replace('/\?v=/i', 'v/', $string);
    PHP:
     
    JAY6390, Feb 13, 2010 IP
  4. ColorWP.com

    ColorWP.com Notable Member

    Messages:
    3,120
    Likes Received:
    100
    Best Answers:
    1
    Trophy Points:
    270
    #4
    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):
     
    ColorWP.com, Feb 13, 2010 IP
  5. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #5
    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 :rolleyes: you need to use a regex to capture the video id and output the video html then I guess using regex
     
    JAY6390, Feb 13, 2010 IP
  6. ColorWP.com

    ColorWP.com Notable Member

    Messages:
    3,120
    Likes Received:
    100
    Best Answers:
    1
    Trophy Points:
    270
    #6
    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?
     
    ColorWP.com, Feb 13, 2010 IP
  7. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #7
    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.
     
    joebert, Feb 13, 2010 IP
  8. ColorWP.com

    ColorWP.com Notable Member

    Messages:
    3,120
    Likes Received:
    100
    Best Answers:
    1
    Trophy Points:
    270
    #8
    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:
     
    ColorWP.com, Feb 14, 2010 IP
  9. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #9
    koko5, Feb 14, 2010 IP
  10. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #10
    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:
     
    danx10, Feb 14, 2010 IP