Forum Moderators: coopster

Message Too Old, No Replies

regex : get one row into one array

         

ayushchd

1:04 pm on Oct 7, 2007 (gmt 0)

10+ Year Member



Hi all.

Currently with this code of mine,

$start_indicate = 'Capital';
$stock_contents = substr($file_contents, strpos($file_contents, $start_indicate) + strlen($start_indicate));
$end_indicate = "</body>";
$stock_contents = substr($stock_contents, 0, strpos($stock_contents, $end_indicate));
$stock_contents = str_replace("</tr>", "*", $stock_contents);
$stock_contents = strip_tags($stock_contents);
$stock_contents = preg_replace("/\n\s*/i", "", $stock_contents);
$stock_contents = explode("*", $stock_contents);
echo "<pre>";
print_r($stock_contents);
echo "</pre>";

I get the following output :


Array(
[0] =>
Structure
[1] =>
Authorised Capital
500.00
500.00
500.00
500.00
[2] =>
Issued Capital
491.65
491.65
491.65
491.65
)

But I want it this way :


Array (
[0] => Structure
)
Array
(
[0] => Authorised Capital
[1] => 500
[2] => 500
[3] => 500
)

Array
(
[0] => Issued Capital
[1] => 491.65
[2] => 491.65
[3] => 491.65
[4] => 491.65
)


Any suggestions?

ayushchd

2:03 pm on Oct 7, 2007 (gmt 0)

10+ Year Member



I finally got it...posting just the main part...

$stock_contents = str_replace("</tr>", "*", $stock_contents);
$stock_contents = preg_replace("/\n\s*/i", "\n", $stock_contents);
$stock_contents = preg_replace('/\s\s+/', "$", $stock_contents);
$stock_contents = strip_tags($stock_contents);
$stock_contents = preg_replace('/\s\n+/', "*", $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>";

The output is like this :

Array
(
[0] =>
[1] => Structure
[2] =>
[3] =>
)
Array
(
[0] =>
[1] =>
[2] =>
[3] => Authorised Capital
[4] => 500.00
[5] => 500.00
[6] => 500.00
[7] => 500.00
[8] =>
)



I dont want those null values in array...how can i remove them?

cameraman

4:46 pm on Oct 7, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



array_filter() [php.net] will do it. It preserves keys, so Structure will still be in [1] and Authorized Capital will still be in [3].
If you want them to be reordered, you could:
array_unshift [php.net]($stock_contents[$i],array_shift [php.net]($stock_contents[$i]));
after the filter.