您可以对strrpos() 和substr() 使用简单的字符串操作,如下所示:
$str = 'http://www.mydomain.co.uk/this-is-the-text-i-would-like-please.php';
echo substr( $str, strrpos( $str, '/') + 1, -4);
这个will output:
this-is-the-text-i-would-like-please
然后,我们用空格替换破折号,使用str_replace(),如下所示:
$str = str_replace( '-', ' ', $str);
To yield:
this is the text i would like please
替代方法是使用parse_url(),然后替换破折号,如下所示:
$str = parse_url( $str, PHP_URL_PATH);
$str = substr( $str, 1, -4); // strip off leading slash and .php from the end
$str = str_replace( '-', ' ', $str);
这也将产生所需的输出。
最后,不用字符串操作的最简单方法是使用pathinfo(),如下所示:
$str = 'http://www.mydomain.co.uk/this-is-the-text-i-would-like-please.php';
$str = pathinfo( $str, PATHINFO_FILENAME);
$str = str_replace( '-', ' ', $str);