In my battery of test strings, I have included all sample strings that you have mentioned in the question and/or in comments.
My pattern will assume that you want to retain the substring from the start of the each string to the end of the first space-separated sequence of strings containing a digit. This is as difficult to explain in plain English as it is to write as a regular expression. :)
Pattern Explanation: Pattern & Replacement Demo
/ #opening pattern delimiter
^ #match from the start of the string
\D* #match zero or more characters that are not a digit
(?: [a-z\d-]*\d[a-z\d-]*)+ #match zero or more letters,digits,hyphens followed by a digit followed by zero or more letters,digits,hyphens
\K #restart the fullstring match (to avoid using a capture group)
.* #match remainder of the string
/ #closing pattern delimiter
i #case-insensitive pattern modifier
Code: (PHP Demo)
$productArr=[
'Dell Inspiron 15 3521 Laptop CDC/ 2GB/ 500GB/ Linux Black Matte Textured Finish',
'Dell Inspiron 15 3521 Laptop CDC/ 4GB/ 500GB/ Win8 Black',
'Nikon D90 DSLR (Black) with AF-S 18-105mm VR Kit Lens',
'Toshiba Satellite L50-B I3010',
'Lenovo Z50-70',
'Apple',
'HP 15-g221AU (Notebook)',
'Acer Aspire E5-571-56UR Notebook'
];
var_export(array_flip(array_flip(preg_replace('/^\D*(?: [a-z\d-]*\d[a-z\d-]*)+\K.*/i','',$productArr))));
Output:
array (
1 => 'Dell Inspiron 15 3521',
2 => 'Nikon D90',
3 => 'Toshiba Satellite L50-B I3010',
4 => 'Lenovo Z50-70',
5 => 'Apple',
6 => 'HP 15-g221AU',
7 => 'Acer Aspire E5-571-56UR',
)
p.s. I am using a double call of array_flip() because:
- It will not damage your values.
- It operates faster than
array_unique().
- It allows the entire method to be chained together in one line.
(You are welcome to use array_unique() if you wish -- same, same.)