I have 2 issues with my below regex:
1) The output shows 2 extra (blank) element at the beginning and end (can't use PREG_SPLIT_NO_EMPTY as isbn can be empty).
2) Is it possible to get associative array from this? I mean I want output in the form of $output = array("title" => "Book1", "writer" => "writer1", "isbn" => "1234") in this format.
$val = "[title]Book1[/title][writer]Writer1[/writer][isbn]1234[/isbn]";
$pattern = '/\[title](.*?)\[\/title].*?\[writer](.*?)\[\/writer].*?\[isbn](.*?)\[\/isbn]/i';
$allparams = preg_split($pattern, $val, -1, PREG_SPLIT_DELIM_CAPTURE);
Output:
Array ( [0] => [1] => Book1 [2] => Writer1 [3] => 1234 [4] => )
preg_splitinstead ofpreg_matchfor a matching job?preg_splitisn't the right function to do that, you need to usepreg_matchwith named captures and to filter numeric keys from the result array.