如何解决PHP-一种“清理”字符串的函数
| 有没有可用的PHP函数用破折号替换字符串中的空格和下划线? 喜欢:Some Word
Some_Word
Some___Word
Some Word
Some ) # $ ^ Word
=>some-word
基本上,经过清理的字符串应仅包含a-z
个字符,数字(0-9
)和破折号(.4ѭ)。
解决方法
这应该产生期望的结果:
$someword = strtolower(preg_replace(\"/[^a-z]+/i\",\"-\",$theword));
, <?php
function sanitize($s) {
// This RegEx removes any group of non-alphanumeric or dash
// character and replaces it/them with a dash
return strtolower(preg_replace(\'/[^a-z0-9-]+/i\',\'-\',$s));
}
echo sanitize(\'Some Word\') . \"\\n\";
echo sanitize(\'Some_Word\') . \"\\n\";
echo sanitize(\'Some___Word\') . \"\\n\";
echo sanitize(\'Some Word\') . \"\\n\";
echo sanitize(\'Some ) # $ ^ Word\') . \"\\n\";
输出:
Some-Word
Some-Word
Some-Word
Some-Word
Some-Word
, 您可能想尝试preg_replace
:
http://php.net/manual/zh/function.preg-replace.php
此页面上的示例:
<?php
$string = \'April 15,2003\';
$pattern = \'/(\\w+) (\\d+),(\\d+)/i\';
$replacement = \'${1}1,$3\';
echo preg_replace($pattern,$replacement,$string);
//April1,2003
?>
您可能想尝试搜索\“使用PHP搜索友好的URL \”,因为有很多文档,例如:
function friendlyURL($string){
$string = preg_replace(\"`\\[.*\\]`U\",\"\",$string);
$string = preg_replace(\'`&(amp;)?#?[a-z0-9]+;`i\',$string);
$string = htmlentities($string,ENT_COMPAT,\'utf-8\');
$string = preg_replace( \"`&([a-z])(acute|uml|circ|grave|ring|cedil|slash|tilde|caron|lig|quot|rsquo);`i\",\"\\\\1\",$string );
$string = preg_replace( array(\"`[^a-z0-9]`i\",\"`[-]+`\"),$string);
return strtolower(trim($string,\'-\'));
}
和用法:
$myFriendlyURL = friendlyURL(\"Barca rejects FIFA statement on Olympics row\");
echo $myFriendlyURL; // will echo barca-rejects-fifa-statement-on-olympics-row
来源:http://htmlblog.net/seo-friendly-url-in-php/
, 不确定@Dagon为什么选择留下评论而不是答案,但这是他答案的扩展。
php \的preg_replace函数允许您用其他任何东西替换任何东西。
这是您的情况的示例:
$input = \"a word 435 (*^(*& HaHa\";
$dashesOnly = preg_replace(\"#[^-a-zA-Z0-9]+#\",$input);
print $dashesOnly; // prints a-word-435-HaHa;
, 您可以考虑在正则表达式的帮助下编写这段代码。
但是我看不到任何可用的功能来帮助您直接将\“ \”替换为\“-\”
, 我在网络上找到了一些有趣的解决方案。.请注意,这些都不是我的代码。只是复制到此处,希望可以帮助您为自己的应用程序构建自定义功能。
这是从Chyrp复制的。应该可以很好地满足您的需求!
/**
* Function: sanitize
* Returns a sanitized string,typically for URLs.
*
* Parameters:
* $string - The string to sanitize.
* $force_lowercase - Force the string to lowercase?
* $anal - If set to *true*,will remove all non-alphanumeric characters.
*/
function sanitize($string,$force_lowercase = true,$anal = false) {
$strip = array(\"~\",\"`\",\"!\",\"@\",\"#\",\"$\",\"%\",\"^\",\"&\",\"*\",\"(\",\")\",\"_\",\"=\",\"+\",\"[\",\"{\",\"]\",\"}\",\"\\\\\",\"|\",\";\",\":\",\"\\\"\",\"\'\",\"‘\",\"’\",\"“\",\"”\",\"–\",\"—\",\"—\",\"–\",\",\"<\",\".\",\">\",\"/\",\"?\");
$clean = trim(str_replace($strip,strip_tags($string)));
$clean = preg_replace(\'/\\s+/\',$clean);
$clean = ($anal) ? preg_replace(\"/[^a-zA-Z0-9]/\",$clean) : $clean ;
return ($force_lowercase) ?
(function_exists(\'mb_strtolower\')) ?
mb_strtolower($clean,\'UTF-8\') :
strtolower($clean) :
$clean;
}
编辑:
我发现更简单的功能!只需几行代码,就可以不言自明了。
function slug($z){
$z = strtolower($z);
$z = preg_replace(\'/[^a-z0-9 -]+/\',\'\',$z);
$z = str_replace(\' \',$z);
return trim($z,\'-\');
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。