问题
I am attempting to remove apostrophes from text and it isn't really working. It's got to be something small.
$text = preg_replace('/\'/', '', $text);
That's what I am using right now to remove it. What am I doing wrong?
There is a series of these to remove special characters to turn them into urls and store them in my database. However, a recent batch appeared with a ' where the ' was.
Any help is greatly appreciated. Thank you in advance.
回答1:
Have a go using str_replace(), it's quicker than preg_replace() since it doesn't use regular expressions.
$text = str_replace("'", '', $text);
回答2:
you can use this regexp to remove apostrophes
$text = preg_replace('/(\'|�*39;)/', '', $text);
also you can use str_replace to remove apostrophes after doing html_entity_decode
$text = str_replace("'","", html_entity_decode($text, ENT_QUOTES));
回答3:
' represents the HTML entity encoding of an apostrophe, i.e. htmlspecialchars($text, ENT_QUOTES)
. You can check for both cases:
$text = "hey this is ' a couple of ' apostrophes with an encoding '";
$text = preg_replace('/�*39;|\'/', '', $text);
// outputs: hey this is a bunch of apostraphes
echo $text;
You can also stick with the str_replace()
equivalent (tends to run faster):
$text = "hey this is ' a couple of ' apostrophes with an encoding '";
$text = str_replace(array("'", "'"), '', $text);
// outputs: hey this is a bunch of apostraphes
echo $text;
回答4:
In addition to the other answers, you may want to check for the unicode representation too?
$result = preg_replace('/([\'\x{0027}]|')/u', '', $subject);
回答5:
How about using string_replace for that, this doesn't require a regular expression.
$sText = preg_match("'", "", $sText);
That being said, the following snippet works as supposed in 5.3:
$text = "woo't";
$text = preg_replace('/\'/', '', $text);
echo $text; // woot
回答6:
Had the same problem and it was stemming from the fact that text was being pasted from MS word which has it's own strange formatting
Solution was to first replace it and other weird characters with something which can be then captured by preg_replace or str_replace, the func below will help with that:
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/7822631/trouble-replacing-apostrophe-with-preg-replace