jokemiri
jokemiri

Reputation: 33

How do I escape quotes in URL?

I want to insert a URL all into single quotes. I have tried using both single and double quotes, but it is not working. I checked this question - Escaping single quote in URL link, but it did not help:

<?php
    echo '\'' base_url().'\'index.php?admin/product/delete/1\'';
    echo '\''; echo base_url().index.php?admin/product/delete/1\'';
    echo '\'. base_url().index.php?admin/product/delete/1\'';
    echo "\'"; echo base_url().'index.php?admin/product/delete/1\'";
?>

I am trying to achieve this: 'http://localhost/my_site/index.php?admin/product/delete/1'

The URL in single quotes.

Upvotes: 1

Views: 2759

Answers (4)

MicE
MicE

Reputation: 5128

You can use double-quotes to pass variables into strings in PHP. On top of enhancing readability, this also saves you the trouble of escaping single quotes.

So for example:

<?php
    $foo = 'bar';

    echo 'single-quoted string: $foo <br />';
    echo "double-quoted string: $foo <br />"; // "$foo" works, "{$foo}" is better
    echo "double-quoted string: \$foo <br />"; // Backslash escapes
?>

Prints (emphasis mine):

single-quoted string: $foo double-quoted string: bar double-quoted string: $foo

So to update your example:

<?php
    /* ... */
    $baseurl = base_url();

    echo "'{$baseurl}index.php?admin/product/delete/1'";
?>

Or how about we put it all into a variable and gain a little readability:

<?php
    /* ... */
    $baseurl = base_url();
    $fullurl = $baseurl . 'index.php?admin/product/delete/1';

    echo "'{$fullurl}'";
?>

Upvotes: 4

ksno
ksno

Reputation: 487

Just simple:

echo "'" . base_url() . "index.php?admin/product/delete/1'";

Upvotes: 4

Johannes
Johannes

Reputation: 67798

If you really want to use only single quotes, try this:

echo '\'' . base_url() . 'index.php?admin/product/delete/1\'';

Upvotes: 5

gbalduzzi
gbalduzzi

Reputation: 10186

echo "'" . base_url() . "index.php?admin/product/delete/1'";

Upvotes: 3

Related Questions