Am I missing something? Why can’t you just use $variable = str_replace( '"', '\\"' ,$variable);
It doesn’t fix it. When there’s speechmarks in the title, it screws up the html.
I thought it would work too.
Can you give me an example of what you want to happen? esmi’s suggestion will replace quotation marks with slash-quotation marks.
Start string: mac’s “title”
Result string: mac\’s \”title\”
Is that not what you want?
That doesn’t work. I currently have this:
$variable = $post->post_title;
$variable = str_replace( "'", "\'", $variable);
$variable = str_replace( '"', '\\"', $variable);
$link = "<a onclick=\"return checkStuff('$variable');\" href='" . wp_nonce_url( get_bloginfo('url') . "/wp-admin/post.php?action=delete&post=" . $post->ID, 'delete-post_' . $post->ID) . "'>".$link."</a>";
I added the following title – Title 1 ” Title 2 “
and the html for the delete link it gave me was this:
<a onclick="return checkStuff('Test 1 \" test="" 2="" \="" );="" href="URL HERE">Delete</a>
but when I add the following title: Title 1 ‘ Title 2’ it gives me the following correctly commented html:
<a onclick="return checkStuff('Title 1 \' Title 2 \' ')" href="URL HERE">Delete</a>
Is using the_title filter an option?
$yourvar = apply_filters( 'the_title' , $post->post_title );
I can’t explain it unless $post->post_title doesn’t contain what you think it does. Maybe it is getting changed somehow before you retrieve it. The str_replace suggested by esmi works as advertised for me. Have you dumped out $variable before the replaces to be sure what it contains?
Little late to post maybe, but did you try using htmlspecialchars?
http://php.net/manual/en/function.htmlspecialchars.php