【问题标题】:php regex prevent replacing $ symbolphp正则表达式防止替换$符号
【发布时间】:2016-03-19 18:26:51
【问题描述】:

我有一个从字符串生成标签的 php 函数:

function generate_tags($text)
{
    $string = preg_replace('/[^\p{L}\p{N}\s]/u', ' ', $text);
    $string = preg_replace('/\s+/', ' ', $string);
    $string = mb_strtolower($string, 'UTF-8');
    $keywords = explode(' ', trim($string));
    foreach($keywords as $key => $value)
    {
        if((strlen($value)) < 1)
        {
            unset($keywords[$key]);
            continue;
        }
    }
    $result = array_unique($keywords);
    $result = array_values($result);
    return $result;
}

字符串是: Ke$ha - We Are Who We Are (Cherry Cole Private Booty) 正式版

但它取代了 $ 符号。如何防止函数出现 $ 符号?

【问题讨论】:

标签: php regex preg-replace


【解决方案1】:

修改第一个preg_replace 以跳过$:

$string = preg_replace('/[^\p{L}\p{N}\s$]/u', ' ', $text);

【讨论】:

  • @DilaraAliyeva 您意识到您只需按下投票按钮下方的勾号即可。你为什么等了 27 分钟才这样做?
  • 我忘记了:/对不起:/
【解决方案2】:

修改第一个 preg_replace 也跳过 $:$string = preg_replace('/[^\p{L}\p{N}\s$]/u', ' ', $text); 您只需按下投票按钮下方的勾号即可。

【讨论】:

  • 为什么复制上一个答案?
猜你喜欢
  • 2020-09-12
  • 1970-01-01
  • 2013-12-09
  • 2011-02-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多