首页 > 解决方案 > 稳健地检测 PHP 字符串中的破折号

问题描述

我需要一种强大的方法来检测字符串中的破折号。下面的代码有时有效,有时无效。

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

在某些情况下,$productcolor 提供STRING(22) "8211; STRIPE STONE"的不仅仅是“STRIPE STONE”。似乎有时破折号被编码为–. 我怎样才能使它更健壮?

标签: phpregex

解决方案


推荐阅读