How can you separate the text from the image URL in PHP when using preg_match_all?

To separate the text from the image URL in PHP when using preg_match_all, you can use capturing groups in the regular expression pattern. By enclosing the parts of the pattern you want to extract in parentheses, you can retrieve them separately in the matches array. This allows you to easily access the text and image URL separately after using preg_match_all.

$text = "This is an example text with an image: <img src='image.jpg'> and some more text.";
$pattern = "/<img src='(.*?)'>/";

preg_match_all($pattern, $text, $matches);

$imageUrl = $matches[1][0];
$textWithoutImageUrl = preg_replace($pattern, '', $text);

echo "Text: " . $textWithoutImageUrl . "\n";
echo "Image URL: " . $imageUrl;