Php preg_match. Add to array

Basically I am trying to use preg_match to find all links with a PDF attachment and then add the whole url to the array. The part I'm struggling with is how to choose everything before the match, right down to quotes <a href="">. I want to do this so that I can iterate over the array and do everything I need with each document. I just want to get '1234.pdf'(plus any information about the auxiliary directory) in an array.

Any ideas?

This is what I have so far, it only returns a match ...

$string1 = "<a href='1234.pdf'>Document 1</a>";

$match = preg_match("/.pdf/i", $string1, $output);

thank

+3
source share
3 answers

DOM , . preg_match, , PDF :

$html = '<a href="foo.pdf">Foo</a>'.
        '<a href="bar.jpg">Bar</a>'.
        '<a href="baz.pdf">Baz</a>';

$dom = new DOMDocument;
$dom->loadHTML($html);
$links = $dom->getElementsByTagName('a');

$result = array();
foreach ($links as $link) {
   $href = $link->getAttribute('href');
   if (preg_match('/\.pdf$/i', $href)) $result[] = $href;
}

print_r($result);

:

Array
(
    [0] => foo.pdf
    [1] => baz.pdf
)
+5

HTML (. netcoder) XPath . , - :

$match = preg_match_all("/(?<=href=['\"])([^'\"]*\\.pdf[^'\"]*)(?=['\"])/",
                        $string1, $output);
+1

If you understand correctly, it looks like you need to use sub-templates. Try something like this ....

$match = preg_match("/href=\"(.*\.pdf)\"/i", $string1, $output);

The $ output variable must be an array with index 0 containing the full text matches, and index 1 containing the text mapped between the brackets.

0
source

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


All Articles