Computing.Net > Forums > Web Development > trim characters from php filehandle

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

trim characters from php filehandle

Reply to Message Icon

Name: jb60606
Date: March 8, 2007 at 10:54:35 Pacific
OS: Suse 10
CPU/Ram: 1Ghz
Product: HP
Comment:

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.



Sponsored Link
Ads by Google

Response Number 1
Name: jb60606
Date: March 8, 2007 at 10:58:08 Pacific
Reply:

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.


0

Response Number 2
Name: Michael J (by mjdamato)
Date: March 8, 2007 at 12:33:52 Pacific
Reply:

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


0

Response Number 3
Name: jb60606
Date: March 8, 2007 at 13:22:31 Pacific
Reply:

Michael,

That worked perfectly. Thanks



0

Response Number 4
Name: jb60606
Date: March 8, 2007 at 13:37:07 Pacific
Reply:

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);


0

Response Number 5
Name: Michael J (by mjdamato)
Date: March 8, 2007 at 16:11:20 Pacific
Reply:

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


0

Related Posts

See More



Response Number 6
Name: jb60606
Date: March 8, 2007 at 18:13:33 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Web Development Forum Home


Sponsored links

Ads by Google


Results for: trim characters from php filehandle

Four characters in PHP www.computing.net/answers/webdevel/four-characters-in-php/2166.html

Can't see MySQL records from PHP www.computing.net/answers/webdevel/cant-see-mysql-records-from-php/353.html

Uploading a file using php www.computing.net/answers/webdevel/uploading-a-file-using-php/2751.html