Forum Moderators: coopster
1 NABIL BANK LIMITED. 13 5320 5275 5280 749 3962745 5300 -20
2 NEPAL INVESTMENT BANK LTD. 21 2000 1995 2000 2489 4977375 2000 (null)
3 STANDARD CHARTERED BANK LTD. 2 6599 6500 6500 23 150490 6700 -200
4 HIMALAYAN BANK LTD. 13 1790 1790 1790 1287 2303730 1800 -10
The place where i have written null, needs to be replaced by 0.
The following code parses it very nicely bt fails to get the null thing...
<?
$file = fopen("today.html", "r");
while(!feof($file))
{
$file_contents .= fread($file, 2048);
}
$start_indicate = '</font></span>';
$stock_contents = substr($file_contents, strpos($file_contents, $start_indicate) + strlen($start_indicate));
$end_indicate = "---------------------------------------------------------------------------------------------------------";
$stock_contents = substr($stock_contents, 0, strpos($stock_contents, $end_indicate));
$stock_contents = trim($stock_contents);
$stock_contents = preg_replace("/\n\s*/i", "\n", $stock_contents);
$stock_contents = preg_replace('/\s\n/', "*", $stock_contents);
$stock_contents = preg_replace('/\s\s+/', "$", $stock_contents);
$stock_contents = explode("*", $stock_contents);
echo "<pre>";
for($i=0;$i<count($stock_contents);$i++)
{
$stock_details = explode( "$", $stock_contents[$i]);
print_r($stock_details);
}
echo "</pre>";
?>
[edited by: PHP_Chimp at 4:13 pm (utc) on Aug. 10, 2007]
Are you getting the data from a database, or some other means? As it may be easier to separate the movement before it gets to the point you are starting from.
<?
//First part of the code as you have it already up to...
$stock_contents = substr($stock_contents, 0, strpos($stock_contents, $end_indicate));
//Divide stock lists into individual lines.
$stock_lines = explode("\n", $stock_contents);
echo "<pre>";
foreach($stock_lines as $stock_line) {
trim($stock_line);
if(preg_match ('{^(\d+)\s+(.*)\s+([-\d]+)\s+([-\d]+)\s+([-\d]+)\s+([-\d]+)\s+([-\d]+)\s+([-\d]+)\s+([-\d]+)\s+([-\d]+)}', $stock_line, $matches)) {
//Line contains row number, stock name, and 8 remaining numbers (each possibly preceded by a -).
$stock_details = array_slice($matches, 1);
} else if(preg_match ('{^(\d+)\s+(.*)\s+([-\d]+)\s+([-\d]+)\s+([-\d]+)\s+([-\d]+)\s+([-\d]+)\s+([-\d]+)\s+([-\d]+)}', $stock_line, $matches)) {
//Line contains row number, stock name, and 7 remaining numbers (each possibly preceded by a -), add 0 as last number.
$stock_details = array_slice($matches, 1);
$stock_details[] = 0;
} else {
//Not a proper stock line, generate error.
}
print_r($stock_details);
}
echo "</pre>";
?>
As at the moment you are looking for something that may or may not be there. There is a changing number of characters before the bit that may or may not be there. The length of the area you are looking for is not constant (it could be 2 or 3 figures then possibly a - sign from the example).
I was hoping that you were not getting each line as in your example. As if you are getting something like the name of the institution, its number then its change then you could use look at each part individually, as at the moment I cant think of a way to sort your problem that does not rely on a lot of assumptions about the data...so you would have to manually check it anyway (so there is no point in that)