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

trim characters from php filehandle

Reply to Message Icon

Original Message
Name: jb60606
Date: March 8, 2007 at 10:54:35 Pacific
Subject: trim characters from php filehandle
OS: Suse 10
CPU/Ram: 1Ghz
Model/Manufacturer: 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.


Report Offensive Message For Removal

Response Number 1
Name: jb60606
Date: March 8, 2007 at 10:58:08 Pacific
Subject: trim characters from php filehandle
Reply: (edit)

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.


Report Offensive Follow Up For Removal

Response Number 2
Name: Michael J (by mjdamato)
Date: March 8, 2007 at 12:33:52 Pacific
Subject: trim characters from php filehandle
Reply: (edit)

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


Report Offensive Follow Up For Removal

Response Number 3
Name: jb60606
Date: March 8, 2007 at 13:22:31 Pacific
Subject: trim characters from php filehandle
Reply: (edit)

Michael,

That worked perfectly. Thanks



Report Offensive Follow Up For Removal

Response Number 4
Name: jb60606
Date: March 8, 2007 at 13:37:07 Pacific
Subject: trim characters from php filehandle
Reply: (edit)

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


Report Offensive Follow Up For Removal

Response Number 5
Name: Michael J (by mjdamato)
Date: March 8, 2007 at 16:11:20 Pacific
Subject: trim characters from php filehandle
Reply: (edit)

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


Report Offensive Follow Up For Removal


Response Number 6
Name: jb60606
Date: March 8, 2007 at 18:13:33 Pacific
Subject: trim characters from php filehandle
Reply: (edit)

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


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: trim characters from php filehandle

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software