如何更改<a>使用php file_get_contents()标记href
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;
相关文章:
- 有没有一种方法可以防止img get请求使用css或js发生
- CORS-重定向到第二个GET正在接收的页面
- jQuery Ajax GET请求工作不正常
- 如何将PHP get查询转换为Meteor's HTTP.get()
- 返回/从Twit's客户端.get
- AngularJS错误:提供程序必须从$get工厂方法返回值
- 如何在router.get()方法中传递url作为参数
- 使用Javascript/JQuery获取JSON GET数据
- 如何使用php文件中的GET来获取我在.js文件中声明的变量
- Get方法获取csv文件的内容
- 什么'$.get和$.ajax之间的区别是什么
- I'我得到这个.get()在ember中未定义
- Babel编译错误:找不到模块核心js/library/fn/get迭代器
- Datatable get列值只给出前10行
- Angularjs http.get() Cross-Origin 请求被阻止
- Angular js http get返回所有页面
- Get JSON from an url
- 将GET请求(HTML字符串)转换为完整的DOM对象
- regex get id imgur embed?
- Mediawiki皮肤创建-Javascript don'Don’t get load