【问题标题】:PHP: Replace Foreign Characters in a StringPHP:替换字符串中的外来字符
【发布时间】:2012-05-13 18:22:01
【问题描述】:
$fileSyntax = strtolower(preg_replace('~&([a-z]{1,2})(acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i', '$1', htmlentities($fileSyntax, ENT_QUOTES, 'UTF-8'))); // remove foreign character accents
$fileSyntax = preg_replace("/[^a-zA-Z0-9\s]/", "", $fileSyntax); // remove anything that's not alphanumeric, or a space
$fileSyntax = preg_replace("/\s+/", "-", $fileSyntax); // replace space with hyphen     
$fileSyntax = trim($fileSyntax, "-"); // removes prefixing and trailing hyphen

上面的代码将产生以下内容:

Pokémon = pokemon
YO MAN! = yo-man

为了提高效率,我想重写它,然后很快将其转换为函数。

我怎样才能使用多个preg_replace(),这样就不会是多行代码了?

【问题讨论】:

  • 4行代码有什么问题?亲爱的开发人员,1 个巨大的行,以及 1 个巨大的 sql 查询不是灵丹妙药。您为开发人员编写代码以便他们可以轻松阅读,因此更喜欢可读性而不是……其他一切
  • 理想情况下我想把它变成一个函数,我想知道我目前解析字符串的方法有多糟糕。
  • “我想知道我目前的方法有多糟糕” - 它是否按预期工作?如果是 - 那么一切都很好。
  • @Aaron - 为什么?我们长大并生活在一个拥有多种语言的社会中?有什么危害?
  • @EdHeal - 我写这个是为了生成有效的 URL,类似于 stackoverflow (php-replace-foreign-characters-in-a-string) 上使用的格式。

标签: php oop function


【解决方案1】:

你知道,这一行:

$fileSyntax = preg_replace("/[^a-zA-Z0-9\s]/", "", $fileSyntax);

应该包含连字符,否则你会阻止人们输入ice-skate,例如它会变成iceskate。

$fileSyntax = preg_replace("/[^a-zA-Z0-9\s-]/", "", $fileSyntax);

真的应该用下划线替换空格(在我看来),因为连字符可以在单词中使用。

你也可以为你的功能这样做:

function replace_chars($fileSyntax){
    return strtolower(
        preg_replace(
            array(
                "/&([a-z]{1,2})(acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);/i",
                "/[^a-zA-Z0-9\s-]/i",
                "/\s+/"
            ),
            array(
                "$1", // remove foreign character accents
                "", // remove anything that's not alphanumeric, hyphen or a space
                "_" // replace space with underscore 
            ), htmlentities($fileSyntax, ENT_QUOTES, 'UTF-8')
        )
    );
}

从技术上讲,这都是一行代码,只是间隔开,以便于阅读和理解正在发生的事情。你可以通过 replace_chars("TeRríbLé(!) STRinG :)"); 调用它,它应该返回 terrible_string

【讨论】:

    【解决方案2】:

    您可以将 preg_replaces 作为主题参数,这样替换返回的内容将是另一个替换的主题,依此类推...

    【讨论】:

      【解决方案3】:

      我认为这个功能可以解决你的部分问题: http://www.php.net/manual/en/function.iconv.php 它会通过替换特殊字符将您的字符串转换为另一个字符集。

      【讨论】:

        【解决方案4】:

        多行代码或函数没有任何问题,阅读起来更清晰,并且与一长行代码一样工作,这是因为如果某些东西是串行,它将保持串行和执行时间是一样的,如果你想加快进程,你可以尝试让 parallel 线程在同一个黑板字符串上工作,但这会相当复杂(你需要解决所有冲突问题)。

        【讨论】:

          【解决方案5】:

          只需使用我的超级功能:

            function text2url($chaine)
              {
              $chaine = htmlentities($chaine, ENT_NOQUOTES, 'utf-8');
              $chaine = preg_replace('#\&([A-za-z])(?:uml|circ|tilde|acute|grave|cedil|ring)\;#', '\1', $chaine);
              $chaine = preg_replace('#\&([A-za-z]{2})(?:lig)\;#', '\1', $chaine);
              $chaine = preg_replace('#\&[^;]+\;#', '', $chaine);
              $chaine = preg_replace('/[^a-zA-Z0-9_ %\[\]\.\(\)%&-]/s', '', $chaine);
              $chaine = str_replace('(', '', $chaine);
              $chaine = str_replace(')', '', $chaine);
              $chaine = str_replace('[', '', $chaine);
              $chaine = str_replace(']', '', $chaine);
              $chaine = str_replace('.', '-', $chaine);
              $chaine = trim($chaine);
              $chaine = str_replace(' ', '_', $chaine);
          
              return $chaine;
              }
          

          【讨论】:

            【解决方案6】:

            还有另一种方法可以只去除字符串中的重音符号。我写了这个函数在我的应用程序上使用,它的语言是葡萄牙语——这意味着它有你能想象到的所有变音符号。它就像一个魅力:

            function stripAccents($string){
                $accents = '/&([A-Za-z]{1,2})(acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml|caron);/';
                $string_encoded = strtolower(htmlentities($string,ENT_NOQUOTES,'UTF-8'));
                return $string_encoded = preg_replace($accents,'$1',$string_encoded);
            

            }

            【讨论】:

              猜你喜欢
              • 2023-03-08
              • 2013-11-05
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多