Here's a copy of my current code:
这是我当前代码的副本:
<?
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;
}
?>
This code is modifying URLs that are shown on my website. Here's a copy of one of the URLs:
此代码正在修改我网站上显示的URL。以下是其中一个网址的副本:
http://example.com/listing/1/Testing-|-See-If-This-Works-
What would I need to change in the code above to remove |
from showing in URLs and to remove the -
at the end of the URL? Any help would be appreciated.
我需要在上面的代码中更改以删除|从URL中显示并删除 - 在URL的末尾?任何帮助,将不胜感激。
1 个解决方案
#1
This will do it:
这样做:
$url = preg_replace('/(\||-$)/', '', $url );
Example:
<?
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;
}
?>
Demo:
Regex explanation:
(\||-$)
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 «$»
#1
This will do it:
这样做:
$url = preg_replace('/(\||-$)/', '', $url );
Example:
<?
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;
}
?>
Demo:
Regex explanation:
(\||-$)
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 «$»