Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14
  1. #1
    New Coder
    Join Date
    Jul 2010
    Posts
    14
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Question How can I search a string and replace only a specific part?

    I'm not a php wizard so forgive me if this is an easy thing, but this is what i'm trying to do. I have a variable which contains a string that is an embed code for youtube. The embed code, by default from youtube, contains a set width="" and height="". I need to search this string to see if it contains width="xxx" and height="xxx" and if it does, replace only the actual numbers for the width and height but keep the rest of the string both before and after.

    Is this possible with some sort of string replace? Will it work if the number for width/height is not the same number of digits as what I need it to be?

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,639
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by brainbuzzmedia View Post
    I'm not a php wizard so forgive me if this is an easy thing, but this is what i'm trying to do. I have a variable which contains a string that is an embed code for youtube. The embed code, by default from youtube, contains a set width="" and height="". I need to search this string to see if it contains width="xxx" and height="xxx" and if it does, replace only the actual numbers for the width and height but keep the rest of the string both before and after.

    Is this possible with some sort of string replace? Will it work if the number for width/height is not the same number of digits as what I need it to be?
    You should be able to use a Regular Expression to do that in a single command.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    New Coder
    Join Date
    Jul 2010
    Posts
    14
    Thanks
    5
    Thanked 0 Times in 0 Posts
    From what I have found for preg_replace() it seems easy to replace a constant value but I can't seem to figure out how to replace a value that changes but starts with a constant locator. Like I can easily use preg_replace() to replace 'width="560"' but only if i know it will be 560. The problem is it could be any number there but i still need to replace the part of the string starting with "width" and ending in the second quotation.

    I'm sure there is a way to do this but I can't figure it out.

  • #4
    Regular Coder
    Join Date
    May 2011
    Posts
    240
    Thanks
    1
    Thanked 56 Times in 55 Posts
    Try the following code

    Code:
    $pattern = '#width="\d+"#i';
    $replace = "";
    $s = '<embed height="100" width="100"></embed>';
    $s = preg_replace($pattern, $replace, $s);

  • Users who have thanked gvre for this post:

    brainbuzzmedia (04-28-2012)

  • #5
    New Coder
    Join Date
    Jul 2010
    Posts
    14
    Thanks
    5
    Thanked 0 Times in 0 Posts
    I don't get it. I'm getting absolutely no output when I echo this. I've tried other varieties I have found online and nothing echos either.

  • #6

  • #7
    New Coder
    Join Date
    Jul 2010
    Posts
    14
    Thanks
    5
    Thanked 0 Times in 0 Posts
    I just did this:

    <?php
    $pattern = '#width="\d+"#i';
    $replace = "";
    $s = '<embed height="100" width="100"></embed>';
    $s = preg_replace($pattern, $replace, $s);
    echo $s;
    ?>

  • #8
    Regular Coder
    Join Date
    May 2011
    Posts
    240
    Thanks
    1
    Thanked 56 Times in 55 Posts
    It does work. If you are echoing to an html page, you could view the html source (ctrl + u) and see the following code:

    Code:
    <embed height="100" ></embed>
    You should add the "src" attribute, in order to see something in the browser window.

  • Users who have thanked gvre for this post:

    brainbuzzmedia (05-11-2012)

  • #9
    New Coder
    Join Date
    Jul 2010
    Posts
    14
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Ack my fault. I was looking for a text output but I didn't realize it would be an <embed>tag.

  • #10
    New Coder
    Join Date
    Jul 2010
    Posts
    14
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Is there any way to also grab the digits inside the quotes for width="d+"? I want to grab just the number value so that i can divide the width by the height to get an aspect ratio to use when resizing them.

  • #11
    Regular Coder
    Join Date
    May 2011
    Posts
    240
    Thanks
    1
    Thanked 56 Times in 55 Posts
    Sure

    Code:
    $pattern = '#width="(\d+)"#i';
    $replace = "";
    $s = '<embed height="100" width="100"></embed>';
    if (preg_match($pattern, $s, $m))
    {
            $width = $m[1];
    
            $s = preg_replace($pattern, $replace, $s);
            echo $s;
    }

  • Users who have thanked gvre for this post:

    brainbuzzmedia (05-11-2012)

  • #12
    New Coder
    Join Date
    Jul 2010
    Posts
    14
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Ok I promise this is the last one!

    Because of youtube's opacity bug, dropdown menus don't show up over the video unless you add "?wmode=opaque" to the end of the src url.

    What I need to do is have a regex to find everything between the quotes in src="", set it to a variable with ?wmode=opaque added at the end and the preg replace the original.

    I know how to do everything now except the regex to find everything (the url) between the quotes in src with a string such as this:

    <iframe width="557" height="390" src="http://www.youtube.com/embed/k1T75jBYeCs?wmode=opaque" frameborder="0" allowfullscreen></iframe>

  • #13
    Regular Coder
    Join Date
    May 2011
    Posts
    240
    Thanks
    1
    Thanked 56 Times in 55 Posts
    Code:
    $pattern = '#<iframe(.+?)src="(http://www.youtube.com/[^"]+)"([^>]*)>#i';
    $s = '<iframe width="557" height="390" src="http://www.youtube.com/embed/k1T75jBYeCs" frameborder="0" allowfullscreen>';
    $s = preg_replace($pattern, '<iframe\\1src="\\2?wmode=opaque"\\3>', $s);
    echo $s;

  • #14
    New Coder
    Join Date
    May 2012
    Posts
    31
    Thanks
    0
    Thanked 7 Times in 7 Posts
    Quote Originally Posted by brainbuzzmedia View Post
    Ok I promise this is the last one!

    Because of youtube's opacity bug, dropdown menus don't show up over the video unless you add "?wmode=opaque" to the end of the src url.

    What I need to do is have a regex to find everything between the quotes in src="", set it to a variable with ?wmode=opaque added at the end and the preg replace the original.

    I know how to do everything now except the regex to find everything (the url) between the quotes in src with a string such as this:

    <iframe width="557" height="390" src="http://www.youtube.com/embed/k1T75jBYeCs?wmode=opaque" frameborder="0" allowfullscreen></iframe>
    Presoanlly, i've hated using preg_replace and anything similar, but heres a shot at it from myself....

    PHP Code:
        $s '<iframe width="640" height="360" src="*insert generic video link*" frameborder="0" allowfullscreen></iframe>';
        
    $srcpat '#[\'"]https?://([^/]+\.)*youtube.com/[^\'"]*[\'"]#siU'
        if (
    preg_match($srcpat$s$match)){
            
            
    $src $match[0];
            
    $src str_replace("\""""$src);
            if(
    strlen(strstr($src"?"))){
                
    $src .= "&wmode=opaque";
            } else {
                
    $src .= "?wmode=opaque";
            }
            echo 
    $sr;
            
        } 
    Amending the previous post from gvre, and finding a google search pattern taht checked a url (as i said, not good with preg stuff), and using an old search string/rename string method i knew of, that is the end result of my attempt.

    If checks to see if there are any querys in the current url (EG: Feature/T/rel/etc) and if it is, it appends "&wmode=opaque", else it appends "?wmode=opaque"

    Looks like gvre beat me to the post :P

    I'll leave mine anyways, because it includes the ability that there might already be some querys in the url.....From what i can tell, his doesnt.
    Last edited by Nikey646; 05-11-2012 at 11:39 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •