问题
I've read a different question on here that addresses this same problem and I can't seem to figure out what's going on here. I'm trying replace an apostrophe in my code with str_replace and it's not working. I've got a string to test:
$clue_question = "If you’re rowin’ the Rhone from start to finish, you begin in this mountain range";
And then some string replaces and their results:
$new_string = str_replace("I", "a", $clue_question)."<br />";
//af you’re rowin’ the Rhone from start to finish, you begin in this mountain range
$another_new_string = str_replace("'", "VB", $clue_question)."<br />";
//If you’re rowin’ the Rhone from start to finish, you begin in this mountain range
$yet_another_new_string = str_replace("'", "VB", $clue_question)."<br />";
//If you’re rowin’ the Rhone from start to finish, you begin in this mountain range
$sdf_another_new_string = str_replace("`", "VB", $clue_question)."<br />";
//If you’re rowin’ the Rhone from start to finish, you begin in this mountain range
$sdf_another_new_string_sdf = str_replace("ï", "VB", $clue_question)."<br />";
//If you’re rowin’ the Rhone from start to finish, you begin in this mountain range
$yet_another = str_replace("’", "sdgd", $clue_question)."<br />";
//If you’re rowin’ the Rhone from start to finish, you begin in this mountain range
$yet_another = str_replace("\’", "sdgd", $clue_question)."<br />";
//If you’re rowin’ the Rhone from start to finish, you begin in this mountain range
I've tried the ASCII equivalents, I've tried escaping them, I've tried them normal. I have no idea why the single apostrophe (or perhaps it's a single quote) is not being replaced in my string. My syntax is fine because the first str_replace() function worked correctly. I've Googled around and the only thing I could think was it was my PHP version after checking another question on here. I ran phpinfo() and I have version 5.2.17 so I'm assuming that's not the issue.
Any ideas? Thank you.
EDIT
My apologies, I originally had a type and had the initial variable as $question instead of $clue_question. This was not the case. I'm able to print out the strings on the screen and have that set correctly, it's just not formatting. Thanks for the activity so far on this question!
回答1:
Note that '
and the like are HTMLisms, methods for expressing character-set-specific figures in something that is text-only. For str_replace()
to understand '
, you would need to convert the HTML to a real character set.
I can't really tell why your non-HTML attempts are failing, but if your input data are encoded in a character set you don't want to handle, then perhaps you need to convert to something more easily managed.
<?php
$q = "If you’re rowin’ the Rhone\n";
print "Original: " . $q;
print "Converted: " . iconv("UTF-8","ASCII//TRANSLIT",$q);
For me, in an xterm that doesn't properly display UTF-8, this gets me the following result:
Original: If youâre rowinâ the Rhone
Converted: If you're rowin' the Rhone
If you can get away with downgrading your input to stuff that always behaves as expected, you may have an easier time manipulating it.
Note that this is NOT a general solution for handling special characters. Using iconv()
to limit your character set may have unintended consequences like removing characters you didn't know were special. Handle with care. Test thoroughly. Always wash your hands after using the bathroom.
回答2:
It appears that you are defining a variable called $question
and doing the replace on a variable called $clue_question
. I just tested the following and it works fine.
<?php
$question = "If you’re rowin’ the Rhone from start to finish, you begin in this mountain range";
$another_new_string = str_replace("’", "VB", $question);
var_dump($another_new_string);
?>
回答3:
Use $question
variable in str_replace() function instead of $clue_question
. Variable name is wrong.
$question = "If you’re rowin’ the Rhone from start to finish, you begin in this mountain range";
echo $yet_another = str_replace("’", "sdgd", $question)."<br />";
Output:
If yousdgdre rowinsdgd the Rhone from start to finish, you begin in this mountain range
回答4:
This is a very old question, but this may be useful for others. The horrible single quote (apostrophe) in my case was generated by MS word, here's a func that replaces it (and other MS word abominations)
function msword_conversion($str)
{
$str = str_replace(chr(130), ',', $str); // baseline single quote
$str = str_replace(chr(131), 'NLG', $str); // florin
$str = str_replace(chr(132), '"', $str); // baseline double quote
$str = str_replace(chr(133), '...', $str); // ellipsis
$str = str_replace(chr(134), '**', $str); // dagger (a second footnote)
$str = str_replace(chr(135), '***', $str); // double dagger (a third footnote)
$str = str_replace(chr(136), '^', $str); // circumflex accent
$str = str_replace(chr(137), 'o/oo', $str); // permile
$str = str_replace(chr(138), 'Sh', $str); // S Hacek
$str = str_replace(chr(139), '<', $str); // left single guillemet
// $str = str_replace(chr(140), 'OE', $str); // OE ligature
$str = str_replace(chr(145), "'", $str); // left single quote
$str = str_replace(chr(146), "'", $str); // right single quote
// $str = str_replace(chr(147), '"', $str); // left double quote
// $str = str_replace(chr(148), '"', $str); // right double quote
$str = str_replace(chr(149), '-', $str); // bullet
$str = str_replace(chr(150), '-–', $str); // endash
$str = str_replace(chr(151), '--', $str); // emdash
// $str = str_replace(chr(152), '~', $str); // tilde accent
// $str = str_replace(chr(153), '(TM)', $str); // trademark ligature
$str = str_replace(chr(154), 'sh', $str); // s Hacek
$str = str_replace(chr(155), '>', $str); // right single guillemet
// $str = str_replace(chr(156), 'oe', $str); // oe ligature
$str = str_replace(chr(159), 'Y', $str); // Y Dieresis
$str = str_replace('°C', '°C', $str); // Celcius is used quite a lot so it makes sense to add this in
$str = str_replace('£', '£', $str);
$str = str_replace("'", "'", $str);
$str = str_replace('"', '"', $str);
$str = str_replace('–', '–', $str);
return $str;
}
Source: https://www.php.net/manual/en/function.str-replace.php
来源:https://stackoverflow.com/questions/23987874/str-replace-not-replacing-apostrophes