我有这些字符串,包含医院和城市的组合。我想分开并获得医院名称。
ABC Hospital (rawalpindi)
ABC Hospital (New) (Rawalpindi)
XYZ Hospital (old) (Lahore)
我想从end分割字符串是一种输出应该是这样的方式。
ABC Hospital
ABC Hospital (New)
XYZ Hospital (old)
答案 0 :(得分:1)
利用子字符串制作一个较小的字符串,其最后一个位置为" "
。
substr($s1, 0, strripos($s1," "));
使用示例代码:
$s1 = "ABC Hospital (rawalpindi)";
$s2 = "ABC Hospital (New) (Rawalpindi)";
$s3 = "XYZ Hospital (old) (Lahore)";
echo substr($s1, 0, strripos($s1," "));
echo substr($s2, 0, strripos($s2," "));
echo substr($s3, 0, strripos($s3," "));
这将产生输出:
ABC Hospital
ABC Hospital (New)
XYZ Hospital (old)
答案 1 :(得分:0)
这是一个简单的正则表达式:
foreach(array('ABC Hospital (rawalpindi)', 'ABC Hospital (New) (Rawalpindi)', 'XYZ Hospital (old) (Lahore))') as $name){
echo preg_replace('/(.*) \(.*/', '\1', $name) . "\n";
}