使用 preg_replace 和 str_ireplace
Using preg_replace and str_ireplace
这是我当前代码的副本:
<?
function smarty_modifier_url(&$url) {
//remove html tags
$url = strip_tags($url);
trim($url);
$url = preg_replace ( '%[.,:\'"/\\[\]{}\%\-_!?]%simx', ' ', $url );
$url = str_ireplace ( " ", "-", $url );
return $url;
}
?>
此代码正在修改我网站上显示的 URL。这是 URL 之一的副本:
http://example.com/listing/1/Testing-|-See-If-This-Works-
我需要在上面的代码中更改什么才能从 URL 中删除 |
并删除 URL 末尾的 -
?任何帮助将不胜感激。
这样做就可以了:
$url = preg_replace('/(\||-$)/', '', $url );
示例:
<?
function smarty_modifier_url(&$url) {
//remove html tags
$url = strip_tags($url);
trim($url);
$url = preg_replace ( '%[.,:\'"/\\[\]{}\%\-_!?]%simx', ' ', $url );
$url = str_replace ( " ", "-", $url );
$url = preg_replace('/(\||-$)/', '', $url );
$url = preg_replace('/[-]{2,}/', '-', $url);
return $url;
}
?>
演示:
正则表达式解释:
(\||-$)
Match the regex below and capture its match into backreference number 1 «(\||-$)»
Match this alternative «\|»
Match the character “|” literally «\|»
Or match this alternative «-$»
Match the character “-” literally «-»
Assert position at the end of the string, or before the line break at the end of the string, if any «$»
这是我当前代码的副本:
<?
function smarty_modifier_url(&$url) {
//remove html tags
$url = strip_tags($url);
trim($url);
$url = preg_replace ( '%[.,:\'"/\\[\]{}\%\-_!?]%simx', ' ', $url );
$url = str_ireplace ( " ", "-", $url );
return $url;
}
?>
此代码正在修改我网站上显示的 URL。这是 URL 之一的副本:
http://example.com/listing/1/Testing-|-See-If-This-Works-
我需要在上面的代码中更改什么才能从 URL 中删除 |
并删除 URL 末尾的 -
?任何帮助将不胜感激。
这样做就可以了:
$url = preg_replace('/(\||-$)/', '', $url );
示例:
<?
function smarty_modifier_url(&$url) {
//remove html tags
$url = strip_tags($url);
trim($url);
$url = preg_replace ( '%[.,:\'"/\\[\]{}\%\-_!?]%simx', ' ', $url );
$url = str_replace ( " ", "-", $url );
$url = preg_replace('/(\||-$)/', '', $url );
$url = preg_replace('/[-]{2,}/', '-', $url);
return $url;
}
?>
演示:
正则表达式解释:
(\||-$)
Match the regex below and capture its match into backreference number 1 «(\||-$)»
Match this alternative «\|»
Match the character “|” literally «\|»
Or match this alternative «-$»
Match the character “-” literally «-»
Assert position at the end of the string, or before the line break at the end of the string, if any «$»