DeShawnT
DeShawnT

Reputation: 479

Why would str_replace not replace question marks?

I have a string with question marks ('?') in it and I want to replace it with something parsable.

However str_replace will not replace any ? characters in my string...

$str = str_replace('?', 'replacement', $str);

Any ideas?

Upvotes: 2

Views: 3235

Answers (2)

jeni
jeni

Reputation: 440

This will replace ? in to space. This may help you.

<?php
$str = "this ? does ? indeed ? work";
$char='';
$str1 = str_replace('?',$char,$str);
echo $str1;
?>

Upvotes: 0

Dan Grossman
Dan Grossman

Reputation: 52372

That code does replace question marks with the word replacement, which means that's not the code you're using, or what's in your string is not a question mark.

PHP's string functions only operate correctly on latin1 (iso-8859-1) encoded strings. In many encodings there may be many codepoints that correspond to a glyph that looks like a question mark visually, but is not the same as ASCII ?.


$str = "Hello? Anyone home?";
$str = str_replace('?', 'replacement', $str);
echo $str;

Output:

Helloreplacement Anyone homereplacement

Upvotes: 7

Related Questions