从PHP中的URL获取可读的字符串

时间:2018-12-18 02:03:01

标签: php regex

给出:

rescue

$URL = "https://somedomain.com/these-are-words/";

我如何获得?:

$URL = "http://somedomain.com/these-are-words/";

3 个答案:

答案 0 :(得分:1)

$url = "https://somedomain.com/these-are-words/";

$path=parse_url($url, PHP_URL_PATH); //extract the path


$words=str_replace('-',' ',trim($path,'/')); //replace the hyphen with spaces and remove the /'s


echo $words; //these are words

答案 1 :(得分:0)

这实际上取决于您拥有的输入字符串的种类,但是类似的事情应该起作用(尽管您可能需要对其进行调整以满足自己的需求):

{{1}}

Demo

答案 2 :(得分:0)

可以尝试类似(?:/[^/]+)+/?$

的操作