次のように、strrpos()
とを使用して単純な文字列操作を使用できます。substr()
$str = 'http://www.mydomain.co.uk/this-is-the-text-i-would-like-please.php';
echo substr( $str, strrpos( $str, '/') + 1, -4);
これは出力します:
this-is-the-text-i-would-like-please
str_replace()
次に、次のように を使用して、ダッシュをスペースに置き換えます。
$str = str_replace( '-', ' ', $str);
降伏するには:
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);