使用 Preg_Replace 替换撇号时遇到问题

发布于 2024-12-10 22:08:19 字数 234 浏览 0 评论 0原文

我试图从文本中删除撇号,但它并没有真正起作用。一定是一件小事。

$text = preg_replace('/\'/', '', $text);

这就是我现在用来删除它的方法。我做错了什么?

有一系列的方法可以删除特殊字符,将它们转换为网址并将它们存储在我的数据库中。然而,最近一批出现了一个' ' 在哪里。

非常感谢任何帮助。先感谢您。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

千纸鹤 2024-12-17 22:08:19

尝试使用 str_replace(),它比 preg_replace() 因为它不使用正则表达式。

$text = str_replace("'", '', $text);

Have a go using str_replace(), it's quicker than preg_replace() since it doesn't use regular expressions.

$text = str_replace("'", '', $text);
探春 2024-12-17 22:08:19

您可以使用此正则表达式删除撇号,

$text = preg_replace('/(\'|�*39;)/', '', $text);

也可以在执行 html_entity_decode 后使用 str_replace 删除撇号

$text = str_replace("'","", html_entity_decode($text, ENT_QUOTES)); 

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)); 
风吹雨成花 2024-12-17 22:08:19

' 表示撇号的 HTML 实体编码,即 htmlspecialchars($text, ENT_QUOTES)。您可以检查这两种情况:

$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;

您也可以坚持使用 str_replace() 等效项(往往运行得更快):

$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;

' 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;
坏尐絯 2024-12-17 22:08:19

除了其他答案之外,您可能还想检查 unicode 表示形式?

$result = preg_replace('/([\'\x{0027}]|')/u', '', $subject);

In addition to the other answers, you may want to check for the unicode representation too?

$result = preg_replace('/([\'\x{0027}]|')/u', '', $subject);
落叶缤纷 2024-12-17 22:08:19

如何使用 string_replace 来实现这一点,这不需要正则表达式。

$sText = preg_match("'", "", $sText);

话虽这么说,以下代码片段按照 5.3 中的假设工作:

$text = "woo't";
$text = preg_replace('/\'/', '', $text);
echo $text; // woot

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
青巷忧颜 2024-12-17 22:08:19

遇到了同样的问题,这是由于文本是从 MS Word 粘贴的,它有自己奇怪的格式解决

方案是首先用可以由 preg_replace 或 str_replace 捕获的东西替换它和其他奇怪的字符,下面的函数将对此有所帮助:

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;
} 

来源:https://www.php.net/manual/en/function.str-replace.php

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文