Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有几个看起来像这样的编码字段:
317+NALON++RD%2C%2BGananoque%2C%2BOntario%2C%2B加拿大
简单的部分是用空格替换“+”。
我的挑战是用空格替换“%2C”或“%2B”。
有时“%”后面的文本可能不同,但总是两个字符。
我尝试使用 str_replace("%**", " ",urlencode($string)) 但没有运气。
有任何想法吗?
str_replace()不处理通配符。您可以为此使用正则表达式(只要您确定要将 ALL %** 更改为空格):
str_replace()
preg_replace("/(%..|\+)+/", ' ', $string);
这将使用您的$string(我认为它已经是 URL 编码的)并将所有 '+' 和 '%**' 替换为空格。请注意,它将用两个空格替换序列 '%2C%2B'(用于两个匹配项)编辑:正则表达式现在匹配任意数量的空格替换,并用一个空格替换所有空格。
$string