簡體   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