Matta301 Matta301 - 11 days ago 5
PHP Question

splitting a string in PHP at various points

I'm in need of help in splitting strings in PHP. I am currently scraping data from another website and i'm trying to split the string that is returned at various different points and save it in an array.

Below is how I want to split it. So I want to split it after the date, before each "(" and after the ")" character. Then store it in an array.

Example of String:

01/11/2016 Aggregated Micro Power Holdings plc (AMPH) JPMorgan Global Growth & Income plc (JPGI) Murray Income Trust plc (MUT)

Example of end result

"date" => "01/11/2016",
"company" => "Aggregated Micro Power Holdings plc",
"epic" => "(AMPH)",
"company" => "JPMorgan Global Growth & Income plc",
"epic" => "(JPGI)",
"company" => "Murray Income Trust plc",
"epic" => "(MUT)",


So far i have tried to use explode to try and split the string after the ")" character.

$string = "01/11/2016 Aggregated Micro Power Holdings plc (AMPH) JPMorgan Global Growth & Income plc (JPGI) Murray Income Trust plc (MUT)";

$array = explode(") ", $string);

echo '<pre>' . var_dump($array) . '</pre>';


If i dump the array i get the following:

array(1) { [0]=> string(129) " 01/11/2016 Aggregated Micro Power Holdings plc (AMPH) JPMorgan Global Growth & Income plc (JPGI) Murray Income Trust plc (MUT) " }

Update*

ok so i have tried using the following to separate the date which has worked.

$spilt = preg_split('/(\d{2}\/\d{2}\/\d{4})/', $string, null, PREG_SPLIT_DELIM_CAPTURE);


I now get the following, but why is value 0 showing empty?

array(3) { [0]=> string(0) "" [1]=> string(10) "01/11/2016" [2]=> string(116) " Aggregated Micro Power Holdings plc (AMPH) JPMorgan Global Growth & Income plc (JPGI) Murray Income Trust plc (MUT)" }

Answer

I didn't understand the Preg_match solutions but I spent 2 days on this and went down a different route by just splitting this string at different places and outputting the results. My solution is:-

$string = '22/11/2016 Iofina (IOF) Gamma Comminucations (GAMA) Ibstock (IBO) Hurricane (HUR) Apple (APPL) Melrose (MRO)';

// Splits the string and returns the date   
$date = preg_split('(\s.*)', $string , null, PREG_SPLIT_DELIM_CAPTURE);
$date = implode('', $date); 

This gives me: //string(10) "22/11/2016"

// Removes the date and splits the string after every ) bracket
$companies = preg_split('([\)])', str_replace(range(0, 9), '', str_replace('/', '', $string)) , null, PREG_SPLIT_DELIM_CAPTURE);

This gives me:
//array(6) { [0]=> string(12) " Iofina (IOF" [1]=> string(27) " Gamma Comminucations (GAMA" [2]=> string(13) " Ibstock (IBO" [3]=> string(15) " Hurricane (HUR" [4]=> string(12) " Apple (APPL" [5]=> string(13) " Melrose (MRO" }

<h3><?php echo $date; ?></h3>
<?php foreach (array_filter($companies) as $key => $value) { ?>

    <?php echo '<p>' . $value . ')' . '</p>'; ?>    

<?php } ?>

The above displays the following

22/11/2016

Iofina (IOF)

Gamma Comminucations (GAMA)

Ibstock (IBO)

Hurricane (HUR)

Apple (APPL)

Melrose (MRO)

Comments