Break the line after a certain word with PHP

Asked

Viewed 163 times

1

Colleagues,

I’m doing an internal search on an existing site, but in one of the results, it’s bringing it up as follows:

 <a href=".....">página 1</a>&nbsp;<a href=".....">página 2</a>&nbsp;<a
 href=".....">página 3</a>

How would I make that after the word < /to> if there was a line break with PHP?

  • Show us what you have tried and what errors you are facing. A tip is using the explode function();

  • após a palavra "" should there be a word between the quotation marks? Or do you want to give a line break at an empty point? There are several ways, it depends a little on how comes the result etc...

  • I didn’t notice... adjusted now. It would be after the word </ a>

  • I’ve tried the explode(). but it always returns me the first line. Remember that not all results bring this way...

  • break at all </a> or only in some specific

  • Break in all, because in this specific result , is bringing one next to the other.

  • 3

    uses str_replace("</a>", "</a><br>", $tua_variavel);

  • It worked Thiago. Thank you!

Show 3 more comments

1 answer

4


I believe this solves for you

$text = '<a href=".....">página 1</a>&nbsp;<a href=".....">página 2</a>&nbsp;<a href=".....">página 3</a>';
$newtext = str_replace('</a>', '</a><br />', $text);

Or if you prefer you can break us &nbsp;

$newtext = str_replace('&nbsp;', '<br />', $text);

problem is it can affect other areas that have this and Voce do not want a line break

  • Perfect... It worked! Thank you!

Browser other questions tagged

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