Robustly detect dash in PHP string [duplicate]

I need a robust way to detect a dash in a string. The code below sometimes works and sometimes don't.

    $currenttitle = $item_name;
    $pattern = '/ [^a-zA-Z0-9 ]+| –/';
    $matches = array();
    $dummy = preg_match($pattern, $currenttitle, $matches);
    $posi = strpos($currenttitle, $matches[0]);
    $productname = substr($currenttitle, 0, $posi);
    $productcolor = substr($currenttitle, $posi+3);
    $dummy = preg_match('/[a-zA-Z0-9 ]+$/', $productcolor, $matchester);
    $productcolor = trim($matchester[0]);


In some cases $productcolor gives STRING(22) "8211; STRIPE STONE" instead of only "STRIPE STONE". It seems that sometimes the dash is encoded to –. How can I make this more robust ?

Read more here:

Content Attribution

This content was originally published by Louis at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: