簡體   English   中英

如何<a>使用php file_get_contents()</a>更改<a>標簽href</a>

[英]How to change <a> tag href using php file_get_contents()

我正在使用file_get_contents( http://example.com/books.php?id=55 )。 我正在使用搜索和替換功能,但無法正常工作。

<?php
$homepage = file_get_contents('http://example.com/category.php?c=55');
echo $homepage;

// Search replace not working
// str_replace("http://localhost/", "http://example.com/", $homepage );

?>

輸出**

<a class="touch" href="/textbook/book1.php">Book1</a>   
<a class="touch" href="/textbook/book2.php"> Book2</a>  
<a class="touch" href="/textbook/book3.php">Book3</a>

如果我單擊url,則顯示http://localhost/textbook/book1.php 但是我想要http://mysite[dot]com/link.php?id=http://example.com/textbook/book1.php

嘗試這個

<?php 
$homepage = file_get_contents('http://example.com/category.php?c=55');
$homepage = str_replace("textbook/", "http://localhost/xx/link.php?id=http://example.com/textbook/", $homepage );
echo $homepage;
?>

您解析的代碼中沒有“ localhost”。 因此,替換“ localhost”將無濟於事。 您只需要使用href找到鏈接並添加自定義鏈接。 另外,HTML中可能有任何文本“教科書”。

嘗試這個:

<?php
$homepage = file_get_contents('http://example.com/category.php?c=55');

$str=str_replace("href=\"", "href=\"http://newsite.com/link.php?id=http://example.com/", $homepage );

echo $str;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM