删除htmlentities并使用urlencode保留文本和空格

时间:2015-07-27 00:24:34

标签: php url seo urlencode html-entities

使用以下脚本创建SEO友好URL;

fit.method

当我检查生成的地址时,它会给我以下内容;

str_replace("%2F", "+", urlencode(@mynameis 'JaySmoke' and I love (Stackoverflow)))

正如你所看到的,urlencode也编码了htmlentities,我想知道是否有办法告诉它忽略所有的htmlentities并且只是编码空间和文本就像;

%40mynameis+%27JaySmoke%27+and+I+love+%28Stackoverflow%29

2 个答案:

答案 0 :(得分:1)

preg_replace应该足够了

preg_replace("@%.{2}@", '', $string)

其中只是替换%加上任意2个字符

答案 1 :(得分:0)

这里的答案很晚

<?php
//$string represents a string text that might contain unwanted characters
$string = $_POST['text-with-all-kind-of-characters'];

//remove all unwanted characters
$res = preg_replace("/[^a-zA-Z0-9 ]/", " ", $string);

//remove extra spaces within the text
$untrimmed = preg_replace('/\s+/', ' ', $res);

//remove extra spaces at ends of string
$trimmed = trim($untrimmed);

//replace all remaining spaces with dash
$urltitle =  str_replace(" ", "+", "$trimmed");

//our final product 
echo $urltitle;


?>