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 3 of 3
  1. #1
    New Coder
    Join Date
    May 2013
    Posts
    40
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Question about using quotes in php

    I am having a problem with one bit of code I made for my website.
    I am using the youtube api to find videos and info on that video. The problem is when the title has like an apostrophe in it which makes the function not work. I tried using double quotes instead of single quote but then I ran into another problem when a video used double quotes somewhere.
    How can I prevent these problems?



    PHP Code:
    echo <<<HTML
    <button onclick="youtube('$titles','$des','$url1')">$titles</button>;

    HTML; 

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    You use addslashes() to prevent the JS code from terminating the strings early.
    Wrap the string or another variable into addslashes when you're assigning the $titles, $des and $url1 variables.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    cpamieta (09-10-2013)

  • #3
    New Coder
    Join Date
    May 2013
    Posts
    40
    Thanks
    14
    Thanked 0 Times in 0 Posts
    Thanks ill try that out

    Still does not seem to work with it. I checked my source here is what the php code created

    Code:
    <button onclick='youtube("GUY MARTIN TT Isle of Man Lap on-board Honda 1000 - 1 of 2","On-board with the TT\'s quickest ever recorded lap in the Senior TT 2007 race in the TT\'s 100th Anniversary Year with British rider, GUY MARTIN (who provides ...","http://www.youtube.com/watch?v=QVXc29ZgutI")'>GUY MARTIN TT Isle of Man Lap on-board Honda 1000 - 1 of 2</button>
    When I remove all the apostrophe then it works.

    PHP Code:
          $title1 Addslashes($titles); 
              
    $dess Addslashes($des);
              
    $url2 Addslashes($url1);
      

    echo <<<html
    <button onclick='youtube("$title1","$dess","$url2")'>$titles</button>


    html; 


    fixed it,
    What I did was that i used str_replace() and had is so it replaced all the " in the string with just a place space.
    Last edited by cpamieta; 09-10-2013 at 07:32 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
    •