Input:
GUJARAT (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?
You can try this
(?<=\s)(\(.*\))
Explanation
(?<=\s) - Positive look behind. Matches \s (a white space).(\(.*\)) - Matches ( some random text)GUJARAT (24) OTHER (14). Not specified by requirements, though. Anways an ungreedy star could be a nice additionIn case you don't want to use regex you can also use simple strpos and 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
Try this one too.
$text="GUJARAT (24)";
$statename= preg_replace("/[(][0-9]*[)]/","",$text);
echo $statename;
{1} seems to be unneded
FOO (123) ... BAR (456)