Brian Tyndall
Brian Tyndall

Reputation: 186

How to add image tags to a string

I am a bit stuck using regex, in a line of text there is an image path, such as:

hello images/image.jpg this is an image

What I am trying to do is to add the HTML img tag to the path, so that it looks like

hello <img src="images/image.jpg" /> this is an image

Any ideas please?

Upvotes: 2

Views: 128

Answers (1)

user142162
user142162

Reputation:

The following will match any chunk of text that ends with .jpg and wrap it in an <img> tag:

preg_replace('/\b\S+\.jpg\b/', '<img src="$0" />', $str);

Upvotes: 2

Related Questions