What would be the simplest but reliable way to parse the src attribute of the first <img> tag found in an arbitrary text string, but without using any external libraries? That means to get everything that is between opening and closing " character of <img> tag's src atrribute.
I did this script, but it is not a reliable solution in some cases:
$string = $item['description'];
$arr = explode('img', $string);
$arr = explode('src', $arr[1]);
$arr = explode('=', $arr[1]);
$arr = explode('>', $arr[1]);
$pos1 = strpos($arr[0], '"')+1;
$pos2 = strrpos($arr[0], '"')-1;
if (!$pos1) {
$pos1 = strpos($arr[0], "'")+1;
$pos2 = strrpos($arr[0], "'")-1;
}
if ($pos1 && $pos2) {
$result = substr($arr[0], $pos1, $pos2);
}
else { $result = null; }
DOMDocumentis not an external library, why not use it? UsegetElementsByTagName(), grab the first item, and get thesrcwith$img->getAttribute('src').