0

I have a text with <br> tags and I want to save it into MySQL database as a new line. not HTML tags.

for example :

$string = 'some text with<br>tags here.'

and I want to save it into MySQL like this :

some text with tags here

what right str_replace for this purpose? thank you.

Saint Robson
  • 5,475
  • 18
  • 71
  • 118

5 Answers5

4

There is already a function in PHP that converts a new line to a br called nl2br(). However, the reverse is not true. Instead you can create your own function like this:

function br2nl($string)
{
    $breaks = array("<br />","<br>","<br/>");  
    return str_ireplace($breaks, "\r\n", $string); 
}

Then whenever you want to use it, just call it as follows:

$original_string = 'some text with<br>tags here.';
$good_string = br2nl($original_string);

There are three things worth mentioning:

  1. It may be better to store the data in the database exactly as the user entered it and then do the conversion when you retrieve it. Of course this depends what you are doing.
  2. Some systems such as Windows use \r\n. Some systems such as Linux and Mac use \n. Some systems such as older Mac systems user \r for new line characters. Given this and especially if you choose to use point 1. above, you might prefer to use the PHP constant PHP_EOL instead of \r\n. This will give the correct new line character no matter what system you are on.
  3. The method I posted above will be more efficient than preg_replace. However, it does not take into account non-standard HTML such as <br /> and other variations. If you need to take into account these variations then you should use the preg_replace() function. With that said, one can overthink all the possible variations and yet still not account for them all. For example, consider <br id="mybreak"> and many other combinations of attributes and white space.
kojow7
  • 10,308
  • 17
  • 80
  • 135
2

You could use str_replace, as you suggest.

$string = 'some text with<br>tags here.';
$string = str_replace('<br>', "\r\n", $string);

Although, if your <br> tags may also be closed, <br /> or <br/>, it may be worth considering using preg_replace.

$string = 'some text with<br>tags here.';
$string = preg_replace('/<br(\s+\/)?>/', "\r\n", $string);
fubar
  • 16,918
  • 4
  • 37
  • 43
1

Here try this. This will replace all <br> to \r\n.

$string = 'some text with<br>tags here.';
str_replace("<br>","\r\n",$string);
echo $string;

Output:

some text with
tags here.
Bluetree
  • 1,324
  • 2
  • 8
  • 25
1

You can use htmlentities— Convert all HTML characters to entities and html_entity_decode to Convert HTML entities to characters

$string = 'some text with<br>tags here'

$a = htmlentities($string);

$b = html_entity_decode($a);

echo $a; // some text with&lt;br&gt;tags here

echo $b; // some text with<br>tags here
A.D.
  • 2,352
  • 2
  • 15
  • 25
1

Try :

mysql_real_escape_string

function safe($value){
    return mysql_real_escape_string($value);
}
sohel14_cse_ju
  • 2,481
  • 9
  • 34
  • 55