Jump to content

[SOLVED] escaping single quotes, double quotes, and semicolons with STR_REPLACE()


Recommended Posts

i have this error code unexpected T STRING

on this line:

str_replace(addslashes('onmouseover="showImg('name', 'tabla_novedades2', 's_asc.png', 'img1');"'), addslashes('onmouseover="showImg('name', 'tabla_novedades2', 's_desc.png', 'img1');"'), $td_name);

 

i tried escaping slashes with the addslashes function but obviously this did not work

there is a ";" in the string so i know that is causing the error

 

I am replacing this string:

'onmouseover="showImg('name', 'tabla_novedades2', 's_asc.png', 'img1');"

 

with this string:

'onmouseover="showImg('name', 'tabla_novedades2', 's_desc.png', 'img1');"

 

how do I do this with no errors with the str_replace function??

In these cases you need to manually escape the quotes. The semi-colon has nothing to do with the error.  It's having unescaped single quotes inside a string delimited by single quotes. Try this:

<?php
str_replace('onmouseover="showImg(\'name\', \'tabla_novedades2\', \'s_asc.png\', \'img1\');"',
                   'onmouseover="showImg(\'name\', \'tabla_novedades2\', \'s_desc.png\', \'img1\');"', $td_name);
?>

 

It might be easier just to do:

<?php
str_replace('s_asc.png','s_desc.png',$td_name);
?>

 

Ken

 

It's a matter of style, but I prefer using temporary variables to nested functions.  Instead of

 

func(func2($a), func2($b));

 

you can use

 

$f_a = func2($a);
$f_b = func2($b);
func($f_a, $f_b);

 

The second version is also easier for debugging for 2 reasons:

 

1.  You can print out intermediate values.

2.  The line number of the error will specify one function, not one set of nested functions.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.