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]);


    var_dump($productcolor);

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: https://stackoverflow.com/questions/62872474/robustly-detect-dash-in-php-string

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: