i have these string that have combination of hospital and cities. i want to split and get only the hospital name.
ABC Hospital (rawalpindi)
ABC Hospital (New) (Rawalpindi)
XYZ Hospital (old) (Lahore)
I want to split the string from end is such a way that output should be.
ABC Hospital
ABC Hospital (New)
XYZ Hospital (old)
Utilizing substring to make a smaller string with the last position of " "
.
substr($s1, 0, strripos($s1," "));
With a sample code:
$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," "));
This will produce the output:
ABC Hospital
ABC Hospital (New)
XYZ Hospital (old)
It's a simple regex:
foreach(array('ABC Hospital (rawalpindi)', 'ABC Hospital (New) (Rawalpindi)', 'XYZ Hospital (old) (Lahore))') as $name){
echo preg_replace('/(.*) \(.*/', '\1', $name) . "
";
}