Oh the dark time before strstr()
...
Here is a selection of some possible methods:
Code: (Demo with PHP5.2.17)
$string='<img src=\'http://www.example.org/images/post_images/2400.jpg\' /><div>© Ham Sammich</div></div>';
//echo 'strstr: ';
//echo strstr($string,'<div>',true); // not in php5.2
echo "strtok: ";
echo strtok($string,'>'),'>';
echo "\n\nsubstr/strpos: ";
echo substr($string,0,strpos($string,'<div>'));
echo "\n\nexplode no limit: ";
//echo explode('<div>',$string)[0]; // not in php5.2
echo current(explode('<div>',$string));
echo "\n\nexplode limit 2: ";
//echo explode('<div>',$string,2)[0]; // not in php5.2
echo current(explode('<div>',$string,2));
echo "\n\npreg_match: ";
//echo preg_match('/<img[^>]*>/',$string,$out)?$out[0]:$string; // not in php5.2
echo preg_match('/<img[^>]*>/',$string,$out)?current($out):$string;
echo "\n\npreg_split: ";
//echo preg_split('/>\K/',$string,2)[0]; // not in php5.2
echo current(preg_split('/>\K/',$string,2));
echo "\n\npreg_replace: ";
echo preg_replace('/<div>.*/','',$string);
Output:
strtok: <img src='http://www.example.org/images/post_images/2400.jpg' />
substr/strpos: <img src='http://www.example.org/images/post_images/2400.jpg' />
explode no limit: <img src='http://www.example.org/images/post_images/2400.jpg' />
explode limit 2: <img src='http://www.example.org/images/post_images/2400.jpg' />
preg_match: <img src='http://www.example.org/images/post_images/2400.jpg' />
preg_split: <img src='http://www.example.org/images/post_images/2400.jpg' />
preg_replace: <img src='http://www.example.org/images/post_images/2400.jpg' />
strstr($string,'<div>',true);
-- this would have been the best call -- one function call creates the desired string, no extra function calls, no excess memory usage / no concatenation.
strtok($string,'>'),'>'
-- this is nearly clean, but not quite because it can only seek a single character and so we must change the target and then concatenate the character at the end.
substr($string,0,strpos($string,'<div>'))
-- it generates the desired substring from the string, but makes two function calls when just one call is desirable for this simple task.
current(explode('<div>',$string))
-- this generates an array of unlimited elements, only to access the first string. It's two function calls and not very clean as it asks for more work to be done than is necessary.
current(explode('<div>',$string,2));
-- limiting the elements is better than the previous explode method, but the same principle applies: why make an array when you only want a string.
preg_match('/<img[^>]*>/',$string,$out)?current($out):'failed';
-- whether it is preg_match, preg_split, preg_replace, etc. regex patterns are notoriously slower than non-regex functions. For this case, there is no advantage to employing a regex approach. So while it is only one function call, it won't be the most efficient way.
Do your own benchmarking if you like, but performance will be indistinguishable to your end user for such a small string.
The next criteria will be up to you. Perhaps you feel one way is easier to read than another, or perhaps you want a function that doesn't choke easily if the expected delimiter is missing.