str_replace

Hi there.

I am trying to use str_replace to strip a variable of it’s apostrophes (single quote marks) but for some reason it isn’t working.

This is the original variable:

	$post_title = mysql_result($query_result, $i, "post_title");

If you look at the page http://ivegotkids.com/newsletters/aries-daily.php you will see under the “Recent Articles” section there are some off characters where it says Top three �yummy mummy� swimsuits. This is because the original post title in the database is Top three ‘yummy mummy’ swimsuits.

To avoid this happening - I want to remove the apostrophes from the returned value of the variable.

I have tried this:

$post_title = mysql_result($query_result, $i, "post_title");
$post_title = str_replace("'", " ", $post_title);

But it has no effect.

I have tried it like this:

$post_title = mysql_result($query_result, $i, "post_title");
$post_title = str_replace("\\'/", " ", $post_title);

Also, no effect.

I have even tried it with preg_replace but that doesn’t work either.

So what is the correct way to strip all apostraphes from the $post_title variable?

They aren’t regular single quotes, but the “fancy” ones ‘ (U+2018) and ’ (U+2019)
Search and replace those and you’ll be fine :slight_smile:

$s = str_replace(‘\’', ‘’, $s);

single quote, backslash, single quote, single quote

Hi there,

I see what you mean - Unfortunately, though, it doesn’t work. I have tried:

	$post_title = mysql_result($query_result, $i, "post_title");
	$post_title = str_replace('’', '', $post_title);
	$post_title = str_replace('‘', '', $post_title); 

And nothing changes. I have also tried just one of the replacements on it’s own in case two together didn’t work like so:

	$post_title = mysql_result($query_result, $i, "post_title");
	$post_title = str_replace('’', '', $post_title); 

But this produced no change either.

Any other ideas?

Sorry - that did not work either.

You can’t use the htmlentities, but should use the uni codes instead.


$post_title = str_replace(array(chr(2018), chr(2019)), '', $post_title);

Thanks for the reply - Unfortunately, that has not worked either.

This is my code now:

 $post_title = mysql_result($query_result, $i, "post_title");
	$post_title = str_replace(array(chr(2018), chr(2019)), '', $post_title);

When I echo the post_title it still comes up with Top three �yummy mummy� swimsuits

Would it be possible to just edit that title? Probably easier than trying to pursuit this route …
If not, run the following code to see what character codes the title actually uses:


for ($i=0; $i<strlen($post_title); $i++) {
    $char = substr($post_title, $i, 1);
    echo '('.$char.'='.ord($char).') ';
}

Then take the codes it outputs and use those in str_replace in combination with chr().

Hi, it is possible to change the post title for that particular post and I have done this now since I couldn’t get either of the previous solutions to work. However, I’m not happy with just leaving it at that because that means it could happen again if any of our site’s editors forget not to copy and paste from MS Word without replacing the punctuation. So I’ll keep a hold of this latest piece of code you’ve given me and run a few tests on our development site and hopefully catch any future mixups before they happen :slight_smile:

Thanks for your help.