繁体   English   中英

如何从PHP DOM中的href =“ animals.html”获取链接?

[英]How do i get links from href=“animals.html” in php DOM?

我的页面是www.example.com/somthing/types.html,www.example.com/somthing2/types.html。此html文件具有标签<a href="animals.html" . somthing,somthing2 <a href="animals.html" . somthing,somthing2是文件,每个文件都包含types.html中的链接和相同文件夹中的animals.html之类的文件。 当我使用dom时,它仅显示“ animals.html”,但我想获取“ www.example.com/somthing/animals.html”。 我如何从www.example.com/somthing/types.html中删除types.html并放入www.example.com/somthing+/animals.html。

我只需要从www.example.com/somthing/types.html删除“ 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/

您也可以按照旧的方式进行操作:

$url = 'www.example.com/somthing/types.html';

$divided = explode( '/', $url );
array_pop( $divided );

$new_url = implode( '/', $divided );
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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM