I
I
Ivan2016-09-16 03:53:45
PHP
Ivan, 2016-09-16 03:53:45

How to parse all filenames with images from HTML code in PHP?

Actually the question is - How to parse all file names with pictures from HTML code in PHP?
I think that you need to use regular expressions, but I'm not strong in them.
If the end of the file name can be determined, for example, by the extensions .png or .jpg, then how to determine where the image file name begins?
I would like an example of a regular expression that, for example, pulls out all the names of pictures with the .jpg extension from the HTML code into an array.
I ask you not to write links to manuals for regular expressions, but simply to help with an example.
Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
X
xmoonlight, 2016-09-16
@iwqn

From my SNIPPET bins :D :

/["']((.*\/\/|)([\/a-z0-9_%]+\.(jpg|png|gif)))["']/g
----------
Находит URL строки с изображениями в HTML:
$1 => путь к изображению
$2 => протокол, если путь АБСОЛЮТНЫЙ
$3 => путь к изображению без протокола, если путь АБСОЛЮТНЫЙ
$4 => расширение изображения (jpg,png или gif)

PHP:
----------------------------
$re = "/[\"']((.*\\/\\/|)([\\/a-z0-9_%]+\\.(jpg|png|gif)))[\"']/"; 


javascript:
------------------------
    var re = /["']((.*\/\/|)([\/a-z0-9_%]+\.(jpg|png|gif)))["']/g; 
    var str = '[TEXT]';
    var m;
     
    while ((m = re.exec(str)) !== null) {
        if (m.index === re.lastIndex) {
            re.lastIndex++;
        }
        // View your result using the m-variable.
        // eg m[0] etc.
    }

PYTHON:
--------------------------
    import re
    p = re.compile(ur'["\']((.*\/\/|)([\/a-z0-9_%]+\.(jpg|png|gif)))["\']')
    test_str = u"[TEXT]"
     
    re.findall(p, test_str)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question