[英]PHP - a function to “sanitize” a string
is there any PHP function available that replaces spaces and underscores from a string with dashes?是否有可用的 PHP function 可用破折号替换字符串中的空格和下划线?
Like:喜欢:
Some Word
Some_Word
Some___Word
Some Word
Some ) # $ ^ Word
=> some-word
=> some-word
basically, the sanitized string should only contain az
characters, numbers ( 0-9
), and dashes ( -
).基本上,净化后的字符串应该只包含az
字符、数字 ( 0-9
) 和破折号 ( -
)。
This should produce the desired result:这应该会产生预期的结果:
$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";
Output: Output:
Some-Word
Some-Word
Some-Word
Some-Word
Some-Word
You might like to try preg_replace
: http://php.net/manual/en/function.preg-replace.php您可能想尝试preg_replace
: http://php.net/manual/en/function.preg-replace.php
Example from this page:此页面的示例:
<?php
$string = 'April 15, 2003';
$pattern = '/(\w+) (\d+), (\d+)/i';
$replacement = '${1}1,$3';
echo preg_replace($pattern, $replacement, $string);
//April1,2003
?>
You might like to try a search for "search friendly URLs with PHP" as there is quite a bit of documentation, example:您可能想尝试搜索“使用 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, '-'));
}
and usage:和用法:
$myFriendlyURL = friendlyURL("Barca rejects FIFA statement on Olympics row");
echo $myFriendlyURL; // will echo barca-rejects-fifa-statement-on-olympics-row
Source: http://htmlblog.net/seo-friendly-url-in-php/来源: http://htmlblog.net/seo-friendly-url-in-php/
Not sure why @Dagon chose to leave a comment instead of an answer, but here's an expansion of his answer.不知道为什么@Dagon 选择发表评论而不是答案,但这是他答案的扩展。
php's preg_replace function allows you to replace anything with anything else. php 的preg_replace function 允许您用其他任何东西替换任何东西。
Here's an example for your case:这是您的案例的示例:
$input = "a word 435 (*^(*& HaHa";
$dashesOnly = preg_replace("#[^-a-zA-Z0-9]+#", "-", $input);
print $dashesOnly; // prints a-word-435-HaHa;
You can think of writing this piece of code with the help of regular expressions.您可以考虑借助正则表达式编写这段代码。
But I dont see any available functions which help you directly replace the " " with "-"但我没有看到任何可用的功能可以帮助您直接将“”替换为“-”
I found a few interesting solutions throughout the web.. note none of this is my code.我在整个 web 中找到了一些有趣的解决方案。请注意,这些都不是我的代码。 Simply copied here in hopes of helping you build a custom function for your own app.在这里简单复制,希望能帮助您为自己的应用程序构建自定义 function。
This has been copied from Chyrp .这是从Chyrp复制的。 Should work well for your needs!应该可以很好地满足您的需求!
/**
* 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;
}
EDIT: Even easier function I found, Just a few lines of code.编辑:更简单的 function 我发现,只需几行代码。 fairly self-explanitory.相当不言自明。
function slug($z){
$z = strtolower($z);
$z = preg_replace('/[^a-z0-9 -]+/', '', $z);
$z = str_replace(' ', '-', $z);
return trim($z, '-');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.