SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 28
  1. #1
    SitePoint Addict aniltc's Avatar
    Join Date
    Nov 2006
    Location
    INDIA
    Posts
    399
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Angry you tube height and width changing

    hi all

    I have a you tube script like this

    <object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/SZfVs2_qoBk&hl=en_US&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/SZfVs2_qoBk&hl=en_US&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>

    I am storing this in my mysql databse.I need to show this in a page.I just passed this into a vriable and displaying ,this is fine.But I need to change the player height and width .How can I do this.

    for eg: in youtube script width="425" and height="344",But I need to change to width="225" and height="144" and it should be displayed.Basically I need to change the width and height,Itried htmlentities and addslases but I am not able to find a solution.please help me.

  2. #2
    Visible Ninja bronze trophy
    JeffWalden's Avatar
    Join Date
    Sep 2002
    Location
    Los Angeles
    Posts
    1,709
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Try this:

    PHP Code:
    $youTube str_replace("width=\"425\"""width=\"225\"""$youTube");
    $youTube str_replace("width=\"344\"""width=\"144\"""$youTube"); 
    http://php.net/manual/en/function.str-replace.php

    I'm sure there's a more sophisticated way to handle this with a single function but this should do the trick.
    TAKE A WALK OUTSIDE YOUR MIND.

  3. #3
    SitePoint Addict aniltc's Avatar
    Join Date
    Nov 2006
    Location
    INDIA
    Posts
    399
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks

    I tried this but notworking...


    <?php
    $youTube="<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/SZfVs2_qoBk&hl=en_US&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/SZfVs2_qoBk&hl=en_US&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>";
    $youTube = str_replace("width=\"344\"", "width=\"144\"", "$youTube");
    echo $youTube;
    ?>


    Parse error: syntax error, unexpected T_LNUMBER in C:\xampplite\htdocs\check\app\views\home\index.ctp on line 2

  4. #4
    Visible Ninja bronze trophy
    JeffWalden's Avatar
    Join Date
    Sep 2002
    Location
    Los Angeles
    Posts
    1,709
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    You'll need to escape the double quotes in your variable.

    $youTube = "<object width=\""500\""></object>";

    You place a backslash \ before the double quote to escape it. As you have quite a few slashes to escape here you could use addslashes();

    $youTube = addslashes($youTube);
    $youTube = str_replace("width=\"344\"", "width=\"144\"", "$youTube");
    echo("$youTube");
    TAKE A WALK OUTSIDE YOUR MIND.

  5. #5
    SitePoint Addict aniltc's Avatar
    Join Date
    Nov 2006
    Location
    INDIA
    Posts
    399
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    not working this way also


    <?php
    $youTube="<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/SZfVs2_qoBk&hl=en_US&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/SZfVs2_qoBk&hl=en_US&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>";
    $youTube = addslashes($youTube);
    $youTube = str_replace("width=\"344\"", "width=\"144\"", "$youTube");
    echo("$youTube");
    ?>

  6. #6
    Visible Ninja bronze trophy
    JeffWalden's Avatar
    Join Date
    Sep 2002
    Location
    Los Angeles
    Posts
    1,709
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    You're still not escaping the quotes in your first variable, $youTube. Notice how you place double quotes around the entire code snippet. That means that if you use double quotes within the variable itself you'll have to escape them with a slash. Alternatively, you could change all of the double quotes within the <object> to single quotes.

    PHP Code:
    $youTube=addslashes("<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/SZfVs2_qoBk&hl=en_US&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/SZfVs2_qoBk&hl=en_US&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>");
    $youTube str_replace("width=\"344\"""width=\"144\"""$youTube");
    $youTube str_replace("height=\"344\"""height=\"144\"""$youTube");
    echo(
    "$youTube"); 
    Untested, not sure if my syntax is correct. The point is that you can't define a variable that has double quotes as part of the value and also use double quotes to define it. You'll need to escape the internal quotes with a slash.
    TAKE A WALK OUTSIDE YOUR MIND.

  7. #7
    SitePoint Addict aniltc's Avatar
    Join Date
    Nov 2006
    Location
    INDIA
    Posts
    399
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    still same error and not working....

  8. #8
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Please post the code that is not working, for further verification.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  9. #9
    SitePoint Addict aniltc's Avatar
    Join Date
    Nov 2006
    Location
    INDIA
    Posts
    399
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi this is my code
    <?php

    $youTube=addslashes("<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/SZfVs2_qoBk&hl=en_US&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/SZfVs2_qoBk&hl=en_US&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>");
    $youTube = str_replace("width=\"344\"", "width=\"144\"", "$youTube");
    $youTube = str_replace("height=\"344\"", "height=\"144\"", "$youTube");
    echo("$youTube");

    ?>

    its not working

  10. #10
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    The double quotes are causing the trouble.

    You need to use single quotes for PHP strings, and double quotes for HTML attributes.

    Here is you current first line for the youtube string
    Code php:
    $youTube=addslashes("<object width="425" height="344">...

    Use a single quote for the string specifier.
    Code php:
    $youTube=addslashes('<object width="425" height="344">...

    Likewise, use a single quote at the end of the string

    Code php:
    ... width="425" height="344"></embed></object>');

    With the rest of the code, using single strings allows you to have cleaner code.

    Instead of this:
    Code php:
    $youTube = str_replace("width=\"344\"", "width=\"144\"", "$youTube");
    $youTube = str_replace("width=\"344\"", "width=\"144\"", "$youTube");

    You will have this:
    Code php:
    $youTube = str_replace('width="344"', 'width="144"', $youTube);
    $youTube = str_replace('height="344"', 'height="144"', $youTube);
    echo("$youTube");

    Do you see how the confusion about what is a PHP string and what are HTML attributes now completely disappears?

    Edit:

    And why is the replacement occurring anyway? You can easily edit the original $youtube string and manually change 344 to 144
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  11. #11
    SitePoint Addict aniltc's Avatar
    Join Date
    Nov 2006
    Location
    INDIA
    Posts
    399
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But video is not showing
    <?php

    $incoming_data=addslashes('$paths_youtube');

    $youTube = str_replace('width="344"', 'width="144"', $incoming_data);
    $youTube = str_replace('height="344"', 'height="144"', $youTube);
    echo $youTube;
    ?>

    here $paths_youtube is from database where I stored my youtube script.

  12. #12
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Remove the quotes from '$paths_youtube' (so there are no quotes at all), and gain some understanding about PHP String Types
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  13. #13
    SitePoint Addict aniltc's Avatar
    Join Date
    Nov 2006
    Location
    INDIA
    Posts
    399
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried but video is not showing

  14. #14
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    What do you think that we need to know to take this troubleshooting further, so that we can help you get it going.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  15. #15
    Visible Ninja bronze trophy
    JeffWalden's Avatar
    Join Date
    Sep 2002
    Location
    Los Angeles
    Posts
    1,709
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Is there no error being displayed?

    If you view the source of the page, do you see any HTML being output?
    TAKE A WALK OUTSIDE YOUR MIND.

  16. #16
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this works
    PHP Code:
    $u '<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/SZfVs2_qoBk&hl=en_US&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/SZfVs2_qoBk&hl=en_US&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>';

    $u str_replace('width="425"''width="999"'$u);
    $u str_replace('height="344"''height="999"'$u);

    echo 
    $u
    if it doesn't for you, then we need more info, error messages? anything show? view source is what?

  17. #17
    SitePoint Addict aniltc's Avatar
    Join Date
    Nov 2006
    Location
    INDIA
    Posts
    399
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi
    you have mentioned like this

    $u = str_replace('width="425"', 'width="999"', $u);
    $u = str_replace('height="344"', 'height="999"', $u);


    width=425;but in my case it is not fixed any width can come form you tube.

    that is the real issue

  18. #18
    Visible Ninja bronze trophy
    JeffWalden's Avatar
    Join Date
    Sep 2002
    Location
    Los Angeles
    Posts
    1,709
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    If that's the case you'll most likely need a regular expression to filter out the string you're looking to replace. Sorry, regular expressions are definitely not my strong point.

    http://php.net/manual/en/function.preg-match.php
    TAKE A WALK OUTSIDE YOUR MIND.

  19. #19
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by aniltc View Post
    hi
    you have mentioned like this

    $u = str_replace('width="425"', 'width="999"', $u);
    $u = str_replace('height="344"', 'height="999"', $u);


    width=425;but in my case it is not fixed any width can come form you tube.

    that is the real issue
    Great to see it only took 17 posts to get to the real issue

    regex will solve this for you, here is a nice site to learn about them, and here is a nice tool to play around with them.

    E: oh, and here is the php library.

  20. #20
    Visible Ninja bronze trophy
    JeffWalden's Avatar
    Join Date
    Sep 2002
    Location
    Los Angeles
    Posts
    1,709
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by hash View Post
    regex will solve this for you, here is a nice site to learn about them, and here is a nice tool to play around with them.

    E: oh, and here is the php library.
    Thanks for the tool! I rarely find myself needing regular expressions but this builder tool will save me loads of frustration.
    TAKE A WALK OUTSIDE YOUR MIND.

  21. #21
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by aniltc View Post
    hi
    you have mentioned like this

    $u = str_replace('width="425"', 'width="999"', $u);
    $u = str_replace('height="344"', 'height="999"', $u);


    width=425;but in my case it is not fixed any width can come form you tube.

    that is the real issue
    preg_replace is what you're after.

    For example:

    Code php:
    $youTube = preg_replace('/(width|height)(="\d+")/', '$1="999"', $youTube);

    That hasn't been tested with live code, but it works on the Regular Expression Tester so it should be pretty good.

    The next question is, are there regex patterns that are "better" than the above? I'm thinking that non-captured groups would do a better job.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  22. #22
    SitePoint Addict aniltc's Avatar
    Join Date
    Nov 2006
    Location
    INDIA
    Posts
    399
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you are changing both width and height with same value.But it is always different

  23. #23
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by aniltc View Post
    you are changing both width and height with same value.But it is always different
    They always start with different values. That is the power of a regular expression.

    "\d+" will match 1 or more digits inside the double quotes. Those digits will be replaced with 999, or whatever number you decide to use.

    For example:

    before: width="345" height="274"
    after: width="999" height="999"

    before: width="193" height="800"
    after: width="999" height="999"

    before: width="987" height="1"
    after: width="999" height="999"
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  24. #24
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by aniltc View Post
    you are changing both width and height with same value.But it is always different
    So use two rules. If you want someone to code it for you, head to the market place. If you have a problem with some regex you have written feel free to post it here.

  25. #25
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by hash View Post
    So use two rules. If you want someone to code it for you, head to the market place. If you have a problem with some regex you have written feel free to post it here.
    I think he's worried that they start out as being different values, and is having a difficult time realising that regular expressions solve that problem.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

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