0

Is it possible to get all image links parsed by Parsedown?
I'm considering something like:

$Parsedown = new Parsedown();
$file = file_get_contents('filename.txt');
echo $Parsedown->text($file);

# output
image1.png
image2.png

filename.txt

![][image1]
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam porttitor nulla id luctus hendrerit.

![](image2.png)
Integer sed ultricies ante, sed mattis mauris. Donec et nisl sapien. 

[image1]: image1.png
Mark Messa
  • 440
  • 4
  • 22

1 Answers1

1

sure, here's the php code for your file example

$re = '/(^|\n)((\[.+\]: )|(!\[.*?\]\())(?<image>.+?\.[^\) ]+)?/';
$str = '![][image1]
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam porttitor nulla id luctus hendrerit.

![](image2.png)
Integer sed ultricies ante, sed mattis mauris. Donec et nisl sapien. 

[image1]: image1.png lorem ipsum

![a](image7.png)

![c](image6.png)

\![a](image5.png)

~~~
![a](image3.png)
~~~

`![a](image4.png)`';

$str = preg_replace('/~~~.*?~~~/s', '', $str);

preg_match_all($re, $str, $matches, PREG_PATTERN_ORDER);
$images = $matches['image'];

then you have an array of all images in $images.

Simon
  • 288
  • 2
  • 6
  • There are some issues with your regex: 1) it matches `[image1]: image1.png Lorem Ipsum`, 2) it matches escaped references – Mark Messa Sep 17 '19 at 14:47
  • I extended the regular expression and the sample string. In the example the resulting images are 1, 2, 6 and 7. – Simon Sep 18 '19 at 08:03