0

I have product names in an array,

$productArr = array("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");

I want to get the output like,

$productModArr = array("Dell Inspiron 15 3521","Dell Inspiron 15 3521","Nikon D90")

And then i need to remove the duplicate string,

$productModArr = array("Dell Inspiron 15 3521","Nikon D90")

I tried substr and strpos but those are not working in my case.

5
  • 2
    Well then you would have to define when the name ends. Commented May 5, 2015 at 14:02
  • Add a delimiter while inserting the name of the laptop in the first play and then use that delimiter to cut the string. Otherwise you can go for the dictionnary approach but it won't handle laptops you won't have filled in the array. Commented May 5, 2015 at 14:05
  • 1
    I'd suggest RegEx for the first part php.net/manual/en/function.preg-match.php and array_unique php.net/manual/de/function.array-unique.php for the second part Commented May 5, 2015 at 14:10
  • @Rizier123 @ Answers_Seeker Thank you,but i got the product names through feed,so i have to create product catelog with modified product names. Commented May 5, 2015 at 14:12
  • @JaGo Thank you,can you please help me with the first part,i can't get the exact preg_match as my product names have no specific ending. Commented May 5, 2015 at 14:37

3 Answers 3

1

You can use the custom PHP function below (employing Regex) to produce your desired output as outlined in your question above.

Depending on the patterns you want to match in your product feed, you may need to tweak the Regex matching pattern a little.

function getProductNames ($productArr) {

$countproductArr = count($productArr);
for ($i = 0; $i < $countproductArr; $i++) {
$productModArr[] = preg_replace('/^([^0-9]*)([0-9]+)(\s[0-9]+)?(.*)/', '$1$2$3', $productArr[$i]);
}

$productModArr = array_unique($productModArr);
$productModArr = array_values($productModArr);
return $productModArr;
}

$productModArr = getProductNames($productArr);

Explanation of the Regex:

/ [... match goes here... ] /

^ - start of match

([^0-9]*) - [first capture group] any non-numerical character, any number of times

([0-9]+) - [second capture group] any numerical character, from 1 to any number of times

(\s[0-9]+)? - [third (optional) capture group] a space followed by any numerical character, from 1 to any number of times

(.*) - any number of characters any number of times following the third (optional) capture group

=======

Update

Replacing the relevant line in the function above with:

$productModArr[] = preg_replace('/^([^0-9]*)([^\s]+)(\s[A-Z]?[0-9]+)?(.*)/', '$1$2$3', $productArr[$i]);

may be a better match for the product name and odel number in more instances.

Sign up to request clarification or add additional context in comments.

7 Comments

If i have these type of strings as well in array, Toshiba Satellite L50-B I3010,Lenovo Z50-70 Ideapad what can i add in pattern?
At this point, you'll need to give us some kind of formal description of the point where you want each string to end. For instance, why do you want to the Toshiba string to end after I3010 but not after L50-B? And why do you want the Lenovo string to end after Ideapad but not after Z50-70? What distinguishes the elements you want to keep in each string from the elements you wish to discard?
Sorry,i don't need ideapad.Actually i am trying to get the product name with model number.
Your script is working fine in most of the cases but if i have a products like HP 15-g221AU (Notebook),Acer Aspire E5-571-56UR Notebook it is matching upto HP 15, Acer Aspire E5 only.I need the complete model number like Acer Aspire E5-571-56UR.
That ought to be possible. Let me rack my brains and see what I can come up with for you...
|
0

This will work for your 3 examples, but since there is no standard in such product names there are probably a lot of situations where the format is different.

$productArr = array("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");
 $newArr = array();
foreach($productArr as $str){
  $pattern = "/[a-zA-Z\ ]*[ A-Z]?[0-9 ]*/";
  preg_match($pattern, $str,$match);
  $newArr[] = $match[0];
}

$result = array_unique($newArr);
print_r($result);

1 Comment

Thank you.I need help for these type of strings as well, Toshiba Satellite L50-B I3010,Lenovo Z50-70.
0

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:

  1. It will not damage your values.
  2. It operates faster than array_unique().
  3. It allows the entire method to be chained together in one line.

(You are welcome to use array_unique() if you wish -- same, same.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.