Delete string-specific word

Asked

Viewed 4,594 times

1

Good evening, everyone. I have a problem which is this::

I need to remove the tag '<br>' at the end of the data in my database (Postgresql), due to the text editor that puts automatically.

I couldn’t create anything capable of doing that, but I imagine if I check the presence of that tag and delete it before displaying it would work (talking is easier than doing, isn’t it? haha)

Anyway, thanks in advance!

  • The <br> only appears at the end?

  • Depends, @rray. Sporadically there may be some in the middle of the string. But what I want to delete is the one at the end.

  • Bank end? Or "value" end of a specific column?

  • the <br> appears at the end of all fields of a table, because I entered this data through a text editor that puts these <br> at the end of each value, understood?

2 answers

4


You can also choose to use this regular expression, which causes the '
' inserted by html,

$string = "Texto com linha <br> e linha 2";
$string = str_replace("<br>", "", $string); 

In MYSQL you can do as follows:

UPDATE tabela SET campo = REPLACE(campo, '<br>', '');
  • André, as well as Matthew’s code, is very helpful! But as I said, it would be interesting to remove the 'br' from the database...

  • Okay, I changed the answer

  • 3

    The answer is good, but you should fix the expression used, because this is not a regular expression (good! string manipulation functions are preferred to regular expressions). And one more suggestion, change the <br> by space, otherwise the line break can "paste" two words improperly. (one can apply a trim to take the spaces from the beginning and the end). However, +1

  • Bacco, as I am working with variables, I am using the concatenation of the variables with commas, points and etc. Gave certinho. But I still hope to remove the tag from the database.

  • @Léoeduardosilva query with UPDATE that André posted should solve the substitution in the BD. If you prefer to make a non-destructive test change, create an extra column in the database and use UPDATE tabela SET campoteste = REPLACE(campo, '<br>', ' ');. If you are happy with the results, simply replicate the data from the new column in the old.

  • It worked perfectly. Just one thing: if I have another line break in the middle of the text (this, in turn, I do not want to delete because it is essential for the text) will also be deleted?

  • You can also use with spacing, ai when replacing, it replaces by space. which also solves

  • This way you will delete only the last <br>? @Andrébaill

Show 3 more comments

2

If I understand this correctly, you can solve it using a regular expression to take the unnecessary '<br>' out of the string.

$texto = "linha 1<br>linha 2<br>";
$texto = preg_replace('(<br>$)', '', $texto);
//remove apenas o <br> do final, mantém os anteriores
  • Matthew did help. But it would be interesting to remove this tag from the database. But I will use your code until I can do that. Thank you so much for the help! I didn’t know about the preg_replace :D function

Browser other questions tagged

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