Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I'm relatively new to PHP and created a portfolio script that downloads quotes from Yahoo.
It does this by using fopen to download the csv file, and fgetcsv to parse it into an array.
The problem is, for the "last trade", it downloads the following string:
"N/A - 10.78"
The price is good, but I obviously do not need the quotation marks, N/A and HTML. Anyone know how I can get rid of them? I've been reading thru the php "trim" function, but I'm not sure if it is what I'm looking for since it only removes special characters from the beginning and end of a string.
The following is the basic code I'm using to get the file:
// open file
$handle = fopen("http://webaddress/filename", "r") or die("Can't open file");
$data = @fgetcsv($handle, 100);
@fclose($handle);
Thanks in advance.

sorry, I don't know how to make HTML code show up. The "10.78" is wrapped in html "bold" tags that I also want to get rid of.

Assuming that there are no scenarios where there will be numbers or periods other than the ones you want to keep, just run the value through the following:
$value = '"N/A - 10.78"';
$value = preg_replace("/[^0-9\.]/", "", $value);
echo $value;This will output 10.78
Michael J

fyi: This is what I ended up putting in the file:
// open yahoo file
$handle = fopen("http://address/file.csv", "r") or die("Can't open file");
$data = @fgetcsv($handle, 100);
$last_trade = $data[1];
@fclose($handle);$last_trimmed = preg_replace("/[^0-9\.]/", "", $last_trade);

Why the extra steps? just do this:
// open yahoo file
$handle = fopen("http://address/file.csv", "r") or die("Can't open file");
$data = @fgetcsv($handle, 100);
@fclose($handle);
$last_trade = preg_replace("/[^0-9\.]/", "", $data[1]);
Michael J

I'm new to the language, and programming in general. Still trying to piece the logic and syntax together. Thanks again.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |