4

The link below works fine unless the variable $row["title"] contains a double-quotation mark ("). In that case, everything after the quotation marks is omitted in the link.

How can I make the link include everything after a double-quotation mark?

Thanks in advance,

John

echo '<td class="sitename2"><a href="http://www...com/.../comments/index.php?submission='.$row["title"].'">'.$row["countComments"].' COMMENTS</a></td>';

5 Answers 5

10

Always use urlencode for parts of a URL which might need to contain anything unusual....

echo '<td class="sitename2">'.
  '<a href="http://www...com/.../comments/index.php?submission='.
  urlencode($row["title"]).
  '">'.
  $row["countComments"].' COMMENTS</a></td>';

If you want to get into the standards, refer to RFC3986 which defines the safe, or 'unreserved' character as follows:

unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"

urlencode makes sure that a value you want to include in a URL obeys these standards

2
  • 1
    FWIW, the newer RFC 3986 says that only -._~ are special (so you may also need to encode !*'()).
    – Carl Walsh
    Nov 19, 2014 at 0:21
  • Thanks Carl, I've amended the answer to refer to the newer RFC
    – Paul Dixon
    Nov 19, 2014 at 12:18
5

Make sure you urlencode your URL parameter values. see urlencode

2

the values of parameters in a url must be escaped in order for the url to be valid.

so instead of

$row["title"]

use

urlencode($row["title"])

urlencode will transform the double-quote into %22 so you will avoid the problem with the wrong double-quote ending your link.

Jerome Wagner

2
. urlencode($row['title']) . '"> etc
1

In my case, urlencode did not help, I used urlencode ($var) and the result in the browser is: localhost/"aaMoldau+-+Aus+dem+Zyklus++"Mein+Vaterland""/

What the browser says:

An Error Was Encountered

The URI you submitted has disallowed characters.

And here is my MYSQL entry ($var):

aaMoldau - Aus dem Zyklus "Mein Vaterland"

update:

the quotes in the beginning and end of $var are clear, because I inserted them, but still the error is there.

(I do not understand why urlencode creates double quotes in the beginning and in the end.)

Furthermore, the double quote before 'Mein' and after Vaterland should be removed ?

Update 2: I now used str_replace to remove the double quotes together with urlencode and it is fine. But I thought I did not need str_replace ?

Furthermore, I read another post of stack where it is recommended to change my allowed uri chars to: $config['permitted_uri_chars'] = '+=\a-z 0-9~%.:_-';

Here CodeIgnite was the problem but now solved. Strange that CI does not accept + in principal which is needed in spaces .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.