0

i have string data like these:

$productList="
Saluran Dua(Bothway)-(TAN007);
Speedy Password-(INET PASS);
Memo-(T-Memo);
7-pib r-10/10-(AM);
FBI (R/N/M)-(Rr);
";

how can i split them, so the result is:

Array1(
[0]=>Saluran Dua(Bothway)
[1]=>Speedy Password
[2]=>Memo
[3]=>7-pib r-10/10
[4]=>FBI (R/N/M)
);

Array2(
[0]=>TAN007
[1]=>INET PASS
[2]=>T-Memo
[3]=>AM
[4]=>Rr
);

i tried preg_match_all, this is for array1:

$separator = '/\-\(([A-z ]*)\)/';
preg_match_all($separator, $productList, $match);
$value=$match[1];

and this is for array2:

$separator2 = '/\;([A-z ]*)\-/';
preg_match_all($separator2, $productList, $desc);
$desc=$desc[1]; 

the result is showed, some of them right and some else disappear.. i believe there's wrong in preg_match_all. i hope you can give me solution..

DimasW
  • 181
  • 1
  • 3
  • 11

1 Answers1

1

I would suggest something like this:

preg_match_all('(^(.*)-\((.*)\);$)m',$productList,$matches);
list(,$value,$desc) = $matches;

Live demo

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • thanks before, but when i run it, it gives me `500 Internal Server Error` . but if i remove it, it run well.. @NiettheDarkAbsol – DimasW Jan 20 '15 at 12:03
  • That's bizarre. Do you have any error logs you can look at to find the cause of the error? What version of PHP are you running? – Niet the Dark Absol Jan 20 '15 at 12:05
  • i use PHP Version 5.4.16 .. no, there's no description of the error.. just show `500 Internal Server Error` then `connection close` . if i remove it, there's no error @NiettheDarkAbsol – DimasW Jan 21 '15 at 02:51
  • the array result is null..sorry @NiettheDarkAbsol , but could you separate it one by one.. preg_match_all for $value first..then for $desc . maybe that cause the error in my code.. – DimasW Jan 21 '15 at 05:12