Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用urlencode删除htmlentities并只留下文本和空间_Php_Url_Seo_Urlencode_Html Entities - Fatal编程技术网

Php 使用urlencode删除htmlentities并只留下文本和空间

Php 使用urlencode删除htmlentities并只留下文本和空间,php,url,seo,urlencode,html-entities,Php,Url,Seo,Urlencode,Html Entities,我创建一个搜索引擎优化友好的网址使用以下脚本 str_replace("%2F", "+", urlencode(@mynameis 'JaySmoke' and I love (Stackoverflow))) 当我检查生成的地址时,它会给我以下信息: %40mynameis+%27JaySmoke%27+and+I+love+%28Stackoverflow%29 正如你所看到的,urlencode也对htmlentities进行了编码,我想知道是否有办法让它忽略所有htmlentiti

我创建一个搜索引擎优化友好的网址使用以下脚本

str_replace("%2F", "+", urlencode(@mynameis 'JaySmoke' and I love (Stackoverflow)))
当我检查生成的地址时,它会给我以下信息:

%40mynameis+%27JaySmoke%27+and+I+love+%28Stackoverflow%29
正如你所看到的,urlencode也对htmlentities进行了编码,我想知道是否有办法让它忽略所有htmlentities,只对空格和文本进行编码

myname+is+JaySmoke+and+I+love+Stackoverflow

一次预更换就足够了

preg_replace("@%.{2}@", '', $string)
它只是将%加上任何2个字符替换为此处的迟交答案

<?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;


?>

我试过了,但没有达到我的要求。它已经停止了转换,但我希望它能将它们全部删除,只留下由加号或减号连接在一起的字母。有点像你在这个页面的url上看到的。已解决。preg_replace(“@%.{2}@”,”,urlencode($string);