[英]PHP : how to remove certain portion of the String in an array
I have this array 我有这个数组
$address[] = $row['name'].",".$row['locality_name'].",".$row['city_name']
The output is 输出是
Ashirwad Estate, Goregaon (w), Mumbai
孟买Goregaon(w)Ashirwad Estate
I want to remove the (w)
string from the output 我想从输出中删除
(w)
字符串
How should I use a function with $row['locality_name']
to trim/remove that? 我应该如何使用带有
$row['locality_name']
的函数来修剪/删除它?
This is a more general answer 这是一个更一般的答案
$row['locality_name'] = preg_replace("/\(.*\)/", "", $row['locality_name']);
or better 或更好
$address[] = sprintf(
"%s,%s,%s",
$row['name'],
preg_replace("/\(.*\)/", "", $row['locality_name']),
$row['city_name']
);
Regex explanation 正则表达式说明
\( // escapes open bracket
.* // matches every char from zero to infinite times
\) // escapes closing bracket
$toRemove = array("(w)","(W)","(West)","(west)");
$row['locality_name'] = str_replace($toRemove, "", $row['locality_name']);
You can use preg_replace
with the following regex 您可以将
preg_replace
与以下正则表达式一起使用
\s*(\([^)]+\)|\-\w+)
So your code looks like as 所以你的代码看起来像
echo preg_replace("/\s*(\([^)]+\)|\-\w+)/","","Ashirwad Estate, Goregaon (w), Mumbai");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.