emsanator 0 Newbie Poster
$html = 'Hello there this is image 1 <img src="http://www.example.com/XXX1.jpg" width="653" height="340" alt="xxx" title="xxx"> !!

And Now you can see image number 2 <img src="http://www.example.com/XXX2.jpg" width="653" height="340" alt="xxx" title="xxx"> yep.';

    $dom = new DOMDocument;
    $dom->loadHTML($html);
    $imgs = $dom->getElementsByTagName('img');
    $imgURLs = [];
    foreach ($imgs as $img) {
        if (!$img->hasAttribute('src')) {
            continue;
        }
        $imgURLs[] = $img->getAttribute('src');
    }
    // print_r($imgURLs);

Result:

Array ( [0] => http://www.example.com/XXX1.jpg [1] => http://www.example.com/XXX2.jpg ) 

I want to change the image links in the text as follows;

{#img='new_Image_1.jpg', alt='Image of Article'}

So:

Hello there this is image 1 {#img='new_Image_1.jpg', alt='Image of Article'} !!
And Now you can see image number 2 {#img='new_Image_2.jpg', alt='Image of Article'} yep.

I could do this if there was only one image link in the text, but I don't know how to change it because there are multiple image links.

Usually, there are 2 images. Available in articles with 1 image. Therefore, an exact number is unfortunately not possible.

How can I do that?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.