简体   繁体   中英

How to remove a href when using get_contents

I am really new to php so still getting to grips.

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.

All help is very much appreciated.

You can use the PHP function strip_tags() like this:

<?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. 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.

you could use "The DOMDocument class" it's used for exactly that. http://php.net/manual/en/class.domdocument.php

you should have the basic idea of oop.

if you struggle with it, you could use strpos, and substr and such, but that would be hard.

strpos: http://php.net/manual/en/function.strpos.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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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