📅  最后修改于: 2023-12-03 14:59:34.392000             🧑  作者: Mango
br2nl
is a simple PHP function that converts HTML line breaks (<br>
, <br/>
, and <br />
) to new lines (\n
) in a string.
This function can be useful when dealing with text entered through an HTML form that includes line breaks. Using br2nl
ensures that the line breaks are properly formatted in the output.
The br2nl
function takes a single parameter, the string to be converted. Here's an example usage:
$text = "Hello<br>world!";
$converted_text = br2nl($text);
echo $converted_text;
The output of this code would be:
Hello
world!
/**
* Converts HTML line breaks to new lines in a string.
*
* @param string $string The string to convert.
* @return string The converted string.
*/
function br2nl($string)
{
return preg_replace('/\<br(\s*)?\/?\>/i', "\n", $string);
}
br2nl
uses a regular expression to find and replace all occurrences of <br>
tags (with or without attributes) with newline characters.
for non-breaking spaces), which may need to be further processed depending on your use case.