简体   繁体   English

使用get_contents时如何删除href

[英]How to remove a href when using get_contents

I am really new to php so still getting to grips. 我对php真的很陌生,因此仍然可以掌握。

I am using this bit of code to pull in world market feed. 我正在使用这段代码来吸引全球市场。

<?php
$homepage = file_get_contents('http://www.news4trader.com/cgi-bin/google_finance.cgi?widget=worldmarkets');
echo $homepage;
?>

I just wanted to know how I can strip the google links out of it so the market titles are just static text. 我只是想知道如何从中剥离google链接,因此市场名称只是静态文本。

All help is very much appreciated. 非常感谢所有帮助。

You can use the PHP function strip_tags() like this: 您可以像这样使用PHP函数strip_tags()

<?php
$homepage = file_get_contents('http://www.news4trader.com/cgi-bin/google_finance.cgi?widget=worldmarkets');
echo strip_tags($homepage, "<style><div><table><tr><td>");
?>

Just include all the tags you want to allow in the second argument. 只需在第二个参数中包含所有要允许的标签即可。

You can use preg_replace() with a regex pattern to filter it out. 您可以将preg_replace()与正则表达式模式一起使用以将其过滤掉。 This is simple, but not very flexible if you want to work more with your loaded data. 这很简单,但是如果您想更多地处理已加载的数据,则不是很灵活。 PHP provides a nice library called DOMDocument (http://php.net/manual/de/class.domdocument.php), with which you can work very flexible on your document. PHP提供了一个很好的库,称为DOMDocument(http://php.net/manual/de/class.domdocument.php),使用该库可以非常灵活地处理文档。

you could use "The DOMDocument class" it's used for exactly that. 您可以完全使用“ DOMDocument类”。 http://php.net/manual/en/class.domdocument.php http://php.net/manual/zh/class.domdocument.php

you should have the basic idea of oop. 您应该具有oop的基本概念。

if you struggle with it, you could use strpos, and substr and such, but that would be hard. 如果您为此而苦苦挣扎,则可以使用strpos和substr等,但这将很难。

strpos: http://php.net/manual/en/function.strpos.php strpos: http : //php.net/manual/zh/function.strpos.php

substr: http://php.net/manual/en/function.substr.php substr: http : //php.net/manual/en/function.substr.php

you can use regex something like this: 您可以使用以下正则表达式:

/<a (.+google.+)>.+<\/a>/

This matches link that has any attribute or value with word google in it 这匹配具有任何属性或值且带有单词google的链接

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

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