How do I escape double quotes in PHP urls?

Discussion in 'PHP' started by seo alchemist, Aug 16, 2008.

  1. #1
    I've been playing with a couple of php scripts that query Google and one of them works fine written as follows:

    $data=file_get_contents("http://www.google.com/search?hl=en&q=allintitle:".$s."&btnG=Google+Search"); 
    
    Code (markup):
    That creates a call to Google emulating a Google search for allintitle:keyword

    The problem arises when I wish instead to implement intitle:"keyword" searches, as in this instance I need the double quotes wraped around the keywords to be included.

    I tried adding another set of double quotes, as shown below, which not surprisingly didn't work, but then nor did escaping them with \ nor changing either set of quotes from single to double or using their ASCI quivalents (apparenlty PHP uses ANSI if that helps).

    $data=file_get_contents("http://www.google.com/search?hl=en&q=allintitle:"".$s.""&btnG=Google+Search"); 
    
    Code (markup):
    I'm sure that I'm missing something obvious, but I can't think what.

    Please help!

    :confused:
     
    seo alchemist, Aug 16, 2008 IP
  2. pluswebdev

    pluswebdev Banned

    Messages:
    205
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #2
    try this
    
    $data=file_get_contents('http://www.google.com/search?hl=en&q=allintitle:"'.$s.'"&btnG=Google+Search');
    
    PHP:
     
    pluswebdev, Aug 16, 2008 IP
  3. Jezz

    Jezz Active Member

    Messages:
    208
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    60
    #3
    Also you can use a backslash to escape it,
    Like this:
    $data=file_get_contents("http://www.google.com/search?hl=en&q=allintitle:\"".$s."\"&btnG=Google+Search");
    Code (markup):
     
    Jezz, Aug 17, 2008 IP
  4. seo alchemist

    seo alchemist Greenhorn

    Messages:
    80
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    23
    #4
    Sorry, as discussed, doesn't work.

    Here's the error message:
    Parse error:  syntax error, unexpected '"' in /usr/local/psa/home/vhosts/marketappeal.co.uk/httpdocs/google-multiple-count-scraper-table-3.php on line 101
    Code (markup):
     
    seo alchemist, Aug 17, 2008 IP
  5. seo alchemist

    seo alchemist Greenhorn

    Messages:
    80
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    23
    #5
    Sorry, but as discussed, this also doesn't appear to work...

    Error message:
    Parse error:  syntax error, unexpected '"' in /usr/local/psa/home/vhosts/marketappeal.co.uk/httpdocs/google-multiple-count-scraper-table-3.php on line 101
    Code (markup):
    :confused:
     
    seo alchemist, Aug 17, 2008 IP
  6. pluswebdev

    pluswebdev Banned

    Messages:
    205
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Sorry but the code posted above by me should work in %100

    As the error message says, check what you have at line 101 in google-multiple-count-scraper-table-3.php
     
    pluswebdev, Aug 17, 2008 IP
  7. nice.wallpapers

    nice.wallpapers Active Member

    Messages:
    142
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    60
    #7
    Hi ,

    Here is the solution :)
    $data=file_get_contents("http://www.google.com/search?hl=en&q=allintitle:\".$s.\"&btnG=Google+Search");

    Thanks :)
     
    nice.wallpapers, Aug 17, 2008 IP
  8. seo alchemist

    seo alchemist Greenhorn

    Messages:
    80
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    23
    #8
    Same as the last suggestion. Still doesn't work, despite being the "official" solution. I don't know why. Any guesses.
     
    seo alchemist, Aug 17, 2008 IP
  9. Jezz

    Jezz Active Member

    Messages:
    208
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    60
    #9
    Sorry seo alchemist,
    But it seems all these methods work. You must have a problem elsewhere.

    <?php
    $s = "Testing...";
    
    $jezz="http://www.google.com/search?hl=en&q=allintitle:\"".$s."\"&btnG=Google+Search";
    $pluswebdev = 'http://www.google.com/search?hl=en&q=allintitle:"'.$s.'"&btnG=Google+Search';
    $nicewallpapers = "http://www.google.com/search?hl=en&q=allintitle:\".$s.\"&btnG=Google+Search";
    $nicewallpapers2 = "http://www.google.com/search?hl=en&q=allintitle:\"$s\"&btnG=Google+Search";
    
    print "<table border=\"1\"><tr><td>Jezz's way :</td><td>".$jezz.'</td><br /></tr>';
    print "<tr><td>pluswebdevs's way  :</td><td>".$pluswebdev.'</td><br /></tr>';
    print "<tr><td>nice wallpapers's way  :</td><td>".$nicewallpapers.'</td><br /></tr>';
    print "<tr><td>nice wallpapers's way (fixed) :</td><td>".$nicewallpapers2.'</td><br /></tr>';
    print "</table>";
    ?>
    Code (markup):
    Copy this code to a blank file, and see for yourself ;)
     
    Jezz, Aug 17, 2008 IP
  10. Kimochi

    Kimochi Peon

    Messages:
    31
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Try the PHP function addslashes().
     
    Kimochi, Aug 17, 2008 IP
  11. seo alchemist

    seo alchemist Greenhorn

    Messages:
    80
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    23
    #11
    Thank you everyone for your solutions.

    It turns out that the problem was that sometimes I was adding one of the escaped " inside the original pair rather than outside them, meaning that the \" was being wrongly interpreted as part of the html rather than as an element of the php.

    Rep points added to everyone who offered help.

    :)
     
    seo alchemist, Aug 17, 2008 IP