PHP Filter to read out Links

Viewed 25

I have a HUGE textfile of about 15MB. It's a JS actually with code inside as well as (Link) references to images. There is a Root URL (www.mywebsite.me/assets/) and between all the code there are links like: "/images1/trees/ujfb6behbweh.jpg" and "/images2/mountains/UZGhbuzg6uhb.png". i need to list all these links as a clickable url:

www.mywebsite.me/assets/images1/trees/ujfb6behbweh.jpg

etc.... how can i achieve this? i know i need to read in the file first

$handle = @fopen("temp.js", "r");
if ($handle)
{
    ....
    fclose($handle);
}

i could filter out anything that starts with "/images1/mountains/... and has the file ending ".png"

im not a complete noob but i am more into C# at the moment. I have been looking for this here, found a few filtering systems but not exactly what i need.

Thank you for Helping me out on this.

1 Answers

What about a regex? php.net function preg_match()

$lines = [
    '/images1/trees/ujfb6behbweh.jpg',
    '/images1/whatever/abiwesdfl1234.gif '
];

foreach ($lines as $line) {
    preg_match('#/images1/\w+/\w+.(png|jpg|gif)#', $line, $matches, PREG_OFFSET_CAPTURE);
    print($matches[0][0] . "\n");
}

output:

/images1/trees/ujfb6behbweh.jpg

/images1/whatever/abiwesdfl1234.gif

Related