Forum Moderators: coopster

Message Too Old, No Replies

replace null bt 0

         

ayushchd

3:08 pm on Aug 10, 2007 (gmt 0)

10+ Year Member



I have something like the following to be parsed :

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

?>

Dragosh

3:15 pm on Aug 10, 2007 (gmt 0)

10+ Year Member



Would this help?
$change = strtr($addr, "null", "0");

[edited by: Dragosh at 3:16 pm (utc) on Aug. 10, 2007]

ayushchd

3:31 pm on Aug 10, 2007 (gmt 0)

10+ Year Member



The place where i have written null....in the original format nuthing's written there//

PHP_Chimp

3:37 pm on Aug 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It is going to be very difficult to match an unknown nothing that could be anywhere...

Is the nothing always in the same space, or is there another way to work out where the nothing is going to be i.e. is the nothing always with the same bank?

ayushchd

3:45 pm on Aug 10, 2007 (gmt 0)

10+ Year Member



its always at the last

PHP_Chimp

4:11 pm on Aug 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



ok so in your example there is a - followed by either 2 or 3 digits at the end of all the others. As there are no other -'s could you just look to see if there is a - and if not add your NULL?
I am guessing that it wont be as simple as that because there may be both + and - at the end?

[edited by: PHP_Chimp at 4:13 pm (utc) on Aug. 10, 2007]

ayushchd

4:20 pm on Aug 10, 2007 (gmt 0)

10+ Year Member



if the value is negative, there is - else no sign

PHP_Chimp

4:41 pm on Aug 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can use strstr or strpos find out if there is a - sign at the end of the string.
However if there may be numbers but no sign then you will need to know the length of the number that may be on the end. As at the moment there is a 4 figure number then the -number at the end of the string.
If that last number could be 4 figures long then im not sure where to go, as you cant just look for a - sign, you cant count backwards that number of figures and look for a space, you cant count forwards as there are different numbers of characters before the -.

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.

borntobeweb

7:39 pm on Aug 10, 2007 (gmt 0)

10+ Year Member



Does this help:

<?
//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>";

?>

PHP_Chimp

9:24 pm on Aug 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



lol I was trying to find out how you are getting the -
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

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)