[英]PHP How to remove word in string?
Input: 输入:
GUJARAT (24)
古吉拉特(24)
Output: 输出:
GUJARAT
古吉拉特邦
String Format 字符串格式
State Name (State Code)
州名(州代码)
I want to remove state code with parentheses. 我想用括号删除州代码。 How can I do this by using regex in PHP?
如何在PHP中使用正则表达式来做到这一点?
Using RegEx 使用RegEx
$text="GUJARAT (24)"
$statename=preg_replace("/\([^)]+\)/","",$text);
Try this one too. 试试这个吧。
$text="GUJARAT (24)";
$statename= preg_replace("/[(][0-9]*[)]/","",$text);
echo $statename;
In case you don't want to use regex you can also use simple strpos and substr. 如果您不想使用正则表达式,您还可以使用简单的strpos和substr。
This is a lighter way to do it than using regex (less meory useage). 这比使用正则表达式更少(使用较少的用法)。
The code finds the (
and removes everything one step behind and forward. 代码找到
(
并删除所有后退和前进的步骤)。
$arr = ["GUJARAT (24)", "State Name (State Code)"];
foreach($arr as $val){
echo substr($val, 0, strpos($val, "(")-1) . "\n";
}
// GUJARAT
// State Name
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.