600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > php 删除特殊符号 从字符串中删除所有特殊字符

php 删除特殊符号 从字符串中删除所有特殊字符

时间:2023-07-27 09:42:24

相关推荐

php 删除特殊符号 从字符串中删除所有特殊字符

下面的解决方案有一个“SEO更友好”版本:functionhyphenize($string){

$dict=array(

"I'm"=>"Iam",

"thier"=>"their",

//Addyourownreplacementshere

);

returnstrtolower(

preg_replace(

array('#[\\s-]+#','#[^A-Za-z0-9\.-]+#'),

array('-',''),

//thefullcleanString()canbedownloadedfrom

/php/php-clean-string-of-utf8-chars-convert-to-similar-ascii-char

cleanString(

str_replace(//preg_replacecanbeusedtosupportmorecomplicatedreplacements

array_keys($dict),

array_values($dict),

urldecode($string)

)

)

)

);}functioncleanString($text){

$utf8=array(

'/[áàâãªä]/u'=>'a',

'/[ÁÀÂÃÄ]/u'=>'A',

'/[ÍÌÎÏ]/u'=>'I',

'/[íìîï]/u'=>'i',

'/[éèêë]/u'=>'e',

'/[ÉÈÊË]/u'=>'E',

'/[óòôõºö]/u'=>'o',

'/[ÓÒÔÕÖ]/u'=>'O',

'/[úùûü]/u'=>'u',

'/[ÚÙÛÜ]/u'=>'U',

'/ç/'=>'c',

'/Ç/'=>'C',

'/ñ/'=>'n',

'/Ñ/'=>'N',

'/–/'=>'-',//UTF-8hyphento"normal"hyphen

'/[’‘‹›‚]/u'=>'',//Literallyasinglequote

'/[“”«»„]/u'=>'',//Doublequote

'//'=>'',//nonbreakingspace(equiv.to0x160)

);

returnpreg_replace(array_keys($utf8),array_values($utf8),$text);}

上述功能的基本原理(我发现方式,道路低效-下面的一个更好)是不应命名的服务显然在URL上进行拼写检查和关键字识别。

在一个顾客的妄想症上失去了很长一段时间之后,我发现他们不想像力毕竟-他们的SEO专家(我绝对不是其中之一)报告说,把“Viaggi Economy Pauro”转换成viaggi-economy-peru“表现更好”viaggi-economy-per(以前的“清洗”删除了UTF 8字符;波哥大成波哥大,麦德林成梅德伦等等)。

还有一些常见的拼写错误似乎影响了结果,对我来说唯一有意义的解释是我们的URL被解压,单词被单独挑出来,用来驱动天知道什么排序算法。这些算法显然都是用UTF 8清理过的字符串来完成的,所以“PEROME”变成了“秘鲁”而不是“PER”。“Per”和“per”不匹配,有点像在脖子上。

为了既保留UTF 8字符又替换一些拼写错误,下面的函数越快,就越准确(?)以上功能。$dict当然需要手工定制。

先前的回答

一个简单的方法:

// Remove all characters except A-Z, a-z, 0-9, dots, hyphens and spaces

// Note that the hyphen must go last not to be confused with a range (A-Z)

// and the dot, being special, is escaped with \

$str = preg_replace('/[^A-Za-z0-9\. -]/', '', $str);

// Replace sequences of spaces with hyphen

$str = preg_replace('/ */', '-', $str);

// The above means "a space, followed by a space repeated zero or more times"

// (should be equivalent to / +/)

// You may also want to try this alternative:

$str = preg_replace('/\\s+/', '-', $str);

// where \s+ means "zero or more whitespaces" (a space is not necessarily the

// same as a whitespace) just to be sure and include everything

请注意,您可能必须首先urldecode()URL,因为%20和+实际上都是空格-我的意思是,如果你有“永不放弃%20给%20 You%20 up”,你希望它变成永远不会放弃的,而不是20..你可能不需要,但我想我应该提一下这个可能性。

因此,完成的功能以及测试用例:functionhyphenize($string){

return

##strtolower(

preg_replace(

array('#[\\s-]+#','#[^A-Za-z0-9\.-]+#'),

array('-',''),

##cleanString(

urldecode($string)

##)

)

##)

;}printimplode("\n",array_map(

function($s){

return$s.'becomes'.hyphenize($s);

},

array(

'Never%20gonna%20give%20you%20up',

"I'mnotthemanIwas",

"'Légeresse',ditsamajesté",

)));Never%20gonna%20give%20you%20upbecomesnever-gonna-give-you-up

I'mnotthemanIwasbecomesim-not-the-man-I-was

'Légeresse',ditsamajestébecomeslegeresse-dit-sa-majeste

为了处理UTF-8,我使用了cleanString实现找到在线(链接被打破,但一个剥离的副本与所有不太深奥的UTF 8字符是在答案的开头;它也很容易添加更多的字符,如果你需要的话),将UTF 8字符转换为正常字符,从而尽可能保留“外观”一词。为了提高性能,可以将其简化并封装在这里的函数中。

上面的函数还实现了转换为小写-但这是一种体验。这样做的代码已被注释掉。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。