I want to export the first image link from given text with PHP. Let’s say I have text like this:
Lorem ipsum <img rel="lorem" src="lorem.jpg"/> dolor sit amet, consectetuer <IMG src="ipsu.jpg" rel="ipsum"/ >
I need to export lorem.jpg to the variable in PHP. So, for example, finally $variable must be equal to lorem.jpg.
I used regular expressions, stripos and so on functions, but everytime there was some problem.
If you have any idea to solve this, please help.
Regular expressions can be good for a large variety of tasks, but it usually fails when parsing HTML DOM. The problem with HTML is that the structure of your document is so variable that it is hard to accurately extract a tag.
We can use DOM parser such as SimpleHTML
You can use it like :
You can also get the alt attribute of the image in the same way.
If you want to get source of all images then you can use:
Hope this helps 🙂 🙂