Any preg_match () for extracting image urls from text?

I need syntax or something similar to extracting JPG or PNG or GIF URLs from blended text and putting them in an array or finally storing the first URL. preg_match()

there may be some syntax that looks for lines starting with http and ending with jpg / png / gif ..

I believe this can be done with preg_match()

Note: the text may be like this: blablablabla " http://www.example.com/xxx.jpg " blablablabla

+3
source share
3 answers

, , .

:

http://www.myserver.com/virus.exe?fakeParam=.jpg

http://www.myserver.com/virus.exe#fakeParam=.jpg

, , , (, % 00 , )

$matches = array();
preg_match_all('!http://[^?#]+\.(?:jpe?g|png|gif)!Ui' , $string , $matches);

, , , , , , , , , :

$matches = array();
preg_match_all('!http://[a-z0-9\-\.\/]+\.(?:jpe?g|png|gif)!Ui' , $string , $matches);

.

+11
$matches = array();
preg_match_all('!http://.+\.(?:jpe?g|png|gif)!Ui' , $string , $matches);
+5

The update for the case is the http / https optional prefix, for example:

http://example.com/image.jpg

https://example.com/image.jpg

//example.com/image.jpg




            function extractImageUrlFromText($text)
            {
                preg_match_all('!(https?:)?//\S+\.(?:jpe?g|jpg|png|gif)!Ui', $text, $matches);
                return $$matches[0];
            }
0
source

Source: https://habr.com/ru/post/1730124/


All Articles