I have created some custom TinyMCE plugins which allow users to insert emojis into the HTML.
The emojis are SVG files and exist in different "emoticons" directories with number 1 to 9 at the end of them, depending on which category of emoji is selected.
The file names always start with an "f" but can be different lengths, and some contain no underscores, some contain 1, others contain 2 or more.
When a user submits content, I put it through HtmlPurifier to make sure it's safe.
Then the end result is stored in a database.
I'd like to be able to extract an array of the different file names which are included with each form submission, so that I can keep track of which files are popular.
For example, if the html looks like this:
<img src="https://example.com/assets/includes/tinymce/plugins/emoticons1/img/fe012.svg" alt="fe012" width="80" height="80" /> and something else and then
<img src="https://example.com/assets/includes/tinymce/plugins/emoticons5/img/f001fg.svg" alt="f001fg" width="63" height="63" /> and we did this
<img src="https://example.com/assets/includes/tinymce/plugins/emoticons2/img/f001_004.svg" alt="f001_004" width="122" height="122" />
<img src="https://example.com/assets/includes/tinymce/plugins/emoticons9/img/f3332.svg" alt="f3332" width="58" height="58" /> maybe something here
<img src="https://example.com/assets/includes/tinymce/plugins/emoticons3/img/f5553_0001.svg" alt="f5553_0001" width="245" height="245" /> going onto
<img src="https://example.com/assets/includes/tinymce/plugins/emoticons4/img/f002a_00d2_fee1.svg" alt="f002a_00d2_fee1" width="30" height="30" />
<img src="https://example.com/assets/includes/tinymce/plugins/emoticons5/img/f3321_a.svg" alt="f3321_a" width="69" height="69" />
Then if I could work out a way to extract a list of file names into an array, I could do something like this:
$files = array("fe012.svg","f001fg.svg", "f001_004.svg", "f3332.svg", "f5553_0001.svg", "f002a_00d2_fee1.svg", "f3321_a.svg");
for($x = 0; $x < count($files); $x++) {
// update database to say image has been used
}
But this is complicated by the fact that:
- The file names are not always the same length
- The HTML for the images is mixed in with other bits of text
- The files are in different directories
The files have this in common:
- The name always starts with "f"
- They are all .svg files
Update 1
Thanks for advice so far, I have got this far:
$html = "<img src=\"https://example.com/assets/includes/tinymce/plugins/emoticons1/img/fe012.svg\" alt=\"fe012\" width=\"80\" height=\"80\" /> and something else and then <img src=\"https://example.com/assets/includes/tinymce/plugins/emoticons5/img/f001fg.svg\" alt=\"f001fg\" width=\"63\" height=\"63\" /> and we did this <img src=\"https://example.com/assets/includes/tinymce/plugins/emoticons2/img/f001_004.svg\" alt=\"f001_004\" width=\"122\" height=\"122\" /> <img src=\"https://example.com/assets/includes/tinymce/plugins/emoticons9/img/f3332.svg\" alt=\"f3332\" width=\"58\" height=\"58\" /> maybe something here <img src=\"https://example.com/assets/includes/tinymce/plugins/emoticons3/img/f5553_0001.svg\" alt=\"f5553_0001\" width=\"245\" height=\"245\" /> going onto <img src=\"https://example.com/assets/includes/tinymce/plugins/emoticons4/img/f002a_00d2_fee1.svg\" alt=\"f002a_00d2_fee1\" width=\"30\" height=\"30\" /> <img src=\"https://example.com/assets/includes/tinymce/plugins/emoticons5/img/f3321_a.svg\" alt=\"f3321_a\" width=\"69\" height=\"69\" />";
$dom = new DOMDocument();
$dom->loadHTML($html);
$images = $dom->getElementsByTagName('img');
foreach($images as $img) {
}
Which is almost there - but how would I then access the image name in the foreach loop?
/(f.*\.svg)/?$dom = new DOMDocument(); $dom->loadHTML("html source here"); $images = $dom->getElementsByTagName('img'); foreach($images as $img) { ... }