我有这个数组
$address[] = $row['name'].",".$row['locality_name'].",".$row['city_name']
输出
Ashirwad Estate,Goregaon(w),孟买
我想从输出中删除(w)
字符串
我应该如何使用带$row['locality_name']
的函数来修剪/删除它?
答案 0 :(得分:1)
这是一个更一般的答案
$row['locality_name'] = preg_replace("/\(.*\)/", "", $row['locality_name']);
或更好
$address[] = sprintf(
"%s,%s,%s",
$row['name'],
preg_replace("/\(.*\)/", "", $row['locality_name']),
$row['city_name']
);
正则表达式解释
\( // escapes open bracket
.* // matches every char from zero to infinite times
\) // escapes closing bracket
答案 1 :(得分:1)
$toRemove = array("(w)","(W)","(West)","(west)");
$row['locality_name'] = str_replace($toRemove, "", $row['locality_name']);
答案 2 :(得分:0)
您可以将include
与以下正则表达式
preg_replace
所以你的代码看起来像
\s*(\([^)]+\)|\-\w+)