tags:

views:

33

answers:

1

Hello guys,

Example url:

http://p.static.com/i/logo.jpg

I want to be able to extract the urls from my own CDN that are referenced in a string, extract the src url and from the src file and folders, so /i/logo.jpg, and then replace this with a hashed version, so any hashing function I may use like encode('/i/logo.jpg'). I'd like an array of all of the matched urls to be returned and then I can process and str_replace inside the string.

A bit complicated, I hope that makes sense.

Thanks

A: 
function encode($val) {
    return preg_replace(
        '/(?<=http:\\/\\/p\\.static\\.com\\/).*/ie',
        'hash_function("\\0")',
        $val);
}

$src = ...; //HTML data

$d = new DOMDocument;
$d->loadHTML($src);

$images = $d->getElementsByTagName("img");
for ($i = 0; $i < $images->length; $i++) {
    $curval = $images->item($i)->getAttribute("src");
    $images->item($i)->setAttribute("src", encode($curval));
}

$newHtml = $d->saveHTML();
Artefacto