PHP preg_replace 特殊字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8483546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:49:55  来源:igfitidea点击:

PHP preg_replace special characters

php

提问by puks1978

I am wanting to replace all non letter and number characters i.e. /&%#$etc with an underscore (_)and replace all '(single quotes) with ""blank (so no underscore).

我想/&%#$用下划线替换所有非字母和数字字符,即等,(_)并将所有'(单引号)替换为“ ”空白(所以没有下划线)。

So "There wouldn't be any" (ignore the double quotes) would become "There_wouldnt_be_any".

所以“不会有任何”(忽略双引号)会变成“ There_wouldnt_be_any”。

I am useless at reg expressions hence the post.

我对 reg 表达式没用,因此这篇文章。

Cheers

干杯

回答by Chris Bornhoft

$newstr = preg_replace('/[^a-zA-Z0-9\']/', '_', "There wouldn't be any");
$newstr = str_replace("'", '', $newstr);

I put them on two separate lines to make the code a little more clear.

我把它们放在两个单独的行上以使代码更清晰一些。

Note: If you're looking for Unicode support, see Filip's answer below. It will match all characters that register as letters in addition to A-z.

注意:如果您正在寻找 Unicode 支持,请参阅下面 Filip 的回答。它将匹配除A-z.之外所有注册为字母的字符。

回答by Filip Roséen - refp

If you by writing "non letters and numbers" exclude more than [A-Za-z0-9](ie. considering letters like ???to be letters to) and want to be able to accurately handle UTF-8 strings \p{L}and \p{N}will be of aid.

如果您通过编写“非字母和数字”排除多个[A-Za-z0-9](即考虑到字母喜欢???是字母 to)并且希望能够准确地处理 UTF-8 字符串\p{L}并且\p{N}会有所帮助。

  1. \p{N}will match any "Number"
  2. \p{L}will match any "Letter Character", which includes
    • Lower case letter
    • Modifier letter
    • Other letter
    • Title case letter
    • Upper case letter
  1. \p{N}将匹配任何“数字
  2. \p{L}将匹配任何“字母字符”,其中包括
    • 小写字母
    • 修饰字母
    • 其他信件
    • 标题大小写字母
    • 大写字母

Documentation PHP: Unicode Character Properties

文档PHP:Unicode 字符属性



$data = "Th?re!wouldn't%bé#?ny";

$new_data = str_replace  ("'", "", $data);
$new_data = preg_replace ('/[^\p{L}\p{N}]/u', '_', $new_data);

var_dump (
  $new_data
);

output

输出

string(23) "Th?re_wouldnt_bé_?ny"

回答by fardjad

do this in two steps:

分两步完成:

  1. replace not letter characters with thisregex:

    [\/\&%#\$]

  2. replace quotes with thisregex:

    [\"\']

  1. 这个正则表达式替换非字母字符:

    [\/\&%#\$]

  2. 这个正则表达式替换引号:

    [\"\']

and use preg_replace:

并使用preg_replace

$stringWithoutNonLetterCharacters = preg_replace("/[\/\&%#$]/", "_", $yourString);
$stringWithQuotesReplacedWithSpaces = preg_replace("/[\"\']/", " ", $stringWithoutNonLetterCharacters);