-1

I have a string that contains HTML-Code. And within that string I am assigning a method WITH AN ARGUMENT to the onclick-attribute. This attribute needs to be a string though.

This is the code. enter image description here

This is how it looks like in HTML. enter image description here

This is how it should look like in HTML. enter image description here

I tried to put double quotes before and after, escape them, escape them with multiple backslahes, and many other things, nothing works. All ideas are appreaciated. :-)

L. Mehmeti
  • 34
  • 6

1 Answers1

1

This looks like a job for stripslashes(). Note however, that you can not use the same double quote (") or single qoutes (') for both the attribute assignment AND the string definition for the parameter. Therefore you want your result to look something like: onclick="removeTag('naruto')".

If you have them as double quotes in your original string (i.e. $row['description']), you can replace those, all put together like this:

echo 'blabla <a href="#" onclick="removeTag(' . str_replace('"', '\'', stripslashes($row['description'])) . ')">';

A tiny bit simpler approach might be to just trim double quotes and add your own single qoutes:

echo 'blabla <a href="#" onclick="removeTag(\'' . trim(stripslashes($row['description']), '"') . '\')">';
ArSeN
  • 5,133
  • 3
  • 19
  • 26