Break line after <img> tag with Regex

Asked

Viewed 82 times

1

Colleagues.

I don’t know much about regular expressions, but I need to know how to break a line after the tag < img > . For example.:

$string = "< img src='images/image.jpg' > here there will be line break";

Would that way:

(the image)
here there will be the line break

  • Hello Bacco... almost the same case. I used your previous example and it worked perfectly, I was able to take the [caption] tag, but now the text has an image and in the return appears the image and next to the texts. I wish that after the image, there was the line break, so that the texts were below the image.

1 answer

3


Follow the Regex:

preg_replace( '/(<\s*img.*>)/U', "$1<br>\n", $string );

See working on IDEONE.


I’m only worried about helping you make the site complicated for nothing, because the cause of the problem seems to be the data in DB in an inappropriate format.

It is suggested to process the data when writing to DB, and not at the time of showing on the screen. It would be even better if you made a special bookmark for your posts, and created the tags and layout from there (as does Markdown, for example).

  • It worked. Thanks again Bacco for the help. In fact we are changing the system by taking out Wordpress and creating another. The problem is that the customer wants to keep the news previously registered by WP and are more than 800 rs rs..

  • Nothing prevents you from creating a script to "fix" these news persists in the bank, it is not necessary to do this on the arm.

  • Exactly, as @mauhumor said, you can instead of using these techniques to show the data on the screen, change the old posts programmatically, so you get everything in the new pattern, without losing the old ones. For example, instead of making PHP change the data in echo, make PHP rewrite old posts in a new database field.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.