note 115625 added to function.str-replace
| From: | markem at sim1 dot us | Date: | Wed, 27 Aug 2014 20:04:34 +0000 |
| Subject: | note 115625 added to function.str-replace | ||
| Groups: | php.notes php.notes | ||
| Request: | Send a blank email to php-notes+get-200326@lists.php.net to get a copy of this message | ||
I was working with MySQL and displaying the title to things on the web page. I'd written a
script to ensure single and double quotes were removed from the title. I used
$title = str_replace( "'", "", $title );
and
$title = str_replace( '"', "", $title );
But still the single and double quotes continued. So I wrote a bit of code to print out each
character separated by a dash. Like so:
for( $i=0; $i<strlen($title); $i++ ){
echo "$i-";
}
echo "<br>\n";
This displayed:
m-y-c-o-m-p-a-n-y- b-b-&-#-3-9-;-s
Which made me go "Oh! I get it."
The MySQL function real_escape_string modifies the single quotes to be ' and double quotes
as " These still show up as single and double quotes under HTML and most importantly -
JAVASCRIPT sees the " and ' as actual single or double
quotes. So if you are passing arguments to a function you have
to get rid of them or else you will get an error on trying to call
a given function. Example:
<a href="javascript:func1('mycompany bbs's")'">
becomes
<a href="javascript:func1('mycompany bbs's');">
Which then will give you an error because there is a single quote inside of the single quoted
string. HOWEVER, the
$title = str_replace( "'", "", $title );
WILL NOT FIND a single quote. Instead, you have to do this:
$title = str_replace( "'", "'", $title );
and
$title = str_relace( """, '"', $title );
(Or you could just get rid of them.)
So remember! If you are trying to remove single and double quotes and are using MySQL and
MySQL's real_escape_string() function that you might be having single and double quotes hanging
around which are defined as ' and " but which show up as single and double quotes as
well as causing problems in your Javascripts.
----
Server IP: 216.194.115.244
Probable Submitter: 66.196.239.162
----
Manual Page -- http://php.net/manual/en/function.str-replace.php
Edit -- https://master.php.net/note/edit/115625
Del: integrated -- https://master.php.net/note/delete/115625/integrated
Del: useless -- https://master.php.net/note/delete/115625/useless
Del: bad code -- https://master.php.net/note/delete/115625/bad+code
Del: spam -- https://master.php.net/note/delete/115625/spam
Del: non-english -- https://master.php.net/note/delete/115625/non-english
Del: in docs -- https://master.php.net/note/delete/115625/in+docs
Del: other reasons-- https://master.php.net/note/delete/115625
Reject -- https://master.php.net/note/reject/115625
Search -- https://master.php.net/manage/user-notes.php