我的页面是www.example.com/somthing/types.html, www.example.com/somthing2/types.html这个html文件有一个标签<a href="animals.html" . somthing,somthing2
是文件,每个文件都包含types.html和类似animals.html的文件中的链接在同一文件夹中。当我得到它与dom它只显示"动物。html",但我想得到"www.example.com/somthing/animals.html"。如何从www.example.com/somthing/types.html中删除types.html并添加www.example.com/somthing+/animals.html.
我只是需要一个方法来删除"types.html"从www.example.com/somthing/types.html。保留文件夹,并删除"/"后面的最后一部分。
我不知道总是最后一部分(文件名),所以我需要一种方法来删除最后一个"/"之后的最后一件事。很抱歉我的语言问题。str_replace('types.html' ,'','www.example.com/somthing/types.html');
是如果我知道文件名(types.html)。
你可以使用
preg_replace('/([^'/]*$)/', '', 'www.example.com/somthing/types.html');
这将替换最后一个/
输出将是www.example.com/somthing/
Try this
$string = 'www.examle.com/somthing/types.html';
echo preg_replace('#'/[^/]*$#', '', $string);
如果您正在考虑最快和最快的,您可以考虑这段代码而不是preg_replace
$url = explode("/",$url);
array_pop($url);
$url = implode("/",$url);
还是稍微快一点,所以你可以随心所欲:)。
证明:http://sandbox.onlinephpfunctions.com/code/5083e02d4f171502ef1e7c87c8dd9a957ee0d8fb
您也可以使用传统的方法:
$url = 'www.example.com/somthing/types.html';
$divided = explode( '/', $url );
array_pop( $divided );
$new_url = implode( '/', $divided );