php - 稳健地检测 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”。似乎有时破折号被编码为–
. 我怎样才能使它更健壮?
解决方案
推荐阅读
- php - PHP/HTML:获取谷歌日历嵌入代码的用户输入
- python - Python 描述符设置和获取行为
- apache-kafka - 我们可以使用 Kafka 消费者直接从 AWS Kinesis Stream 读取数据吗?
- java - 从 SortedSet 在 Java 中创建一定大小的列表
- angular - 消失 queryParams 路由器 Angular
- r - 在R中设置highcharter柱形图列的粗细
- mysql - 跨层次表查询 SQL
- java - 使用 java 8 查找重复项
- python - OpenCV Python中是否有类似pil putpalette的功能?
- flutter - Flutter 条件小部件但 {} 不等于 {}