In this article, we are given a multiline string and the task is to replace line breaks with <br> tag.
Example:
Input: `Geeks for Geeks is a computer science portal where people study computer science` Output: "Geeks for Geeks is <br> a computer science portal <br> where people study computer science"To achieve this we have the following methods :
Method 1: Using Regex, in this example, we are creating a paragraph and a button and on clicking the button we are changing (Adding the <br>)the text of the paragraph.
We are using the String.replace() method of regex to replace the new line with <br>. The String.replace() is an inbuilt method in JavaScript that is used to replace a part of the given string with another string or a regular expression. The original string will remain unchanged.
Example 1: This example shows the use of string.replace() method to replace the new line with <br>.
HTML
<p id="para"></p>
<<1 <2=<4<5<1>
<<9>
p1p2
p3p4
p3p6
p7
p1p9
p7
p1id2
p7
p1id5
id6id7id8>
id6=1id8=3
p7
id6=6
id6id2
p1"para"0
"para"1<9>
Output:
Replace line breaks with br tag
Method 2: Using split() and join(), in this method, we split the string from the delimiter “\n” which returns an array of substrings, and then we join the Array using the join method and pass <br /> so that every joining contains <br />.
Example 2: In this example, we will use the javascript split() and join() method to replace the new line with
Here's a PHP version of print_r which can be tailored to your needs. Shows protected and private properties of objects and detects recursion (for objects only!). Usage:
void u_print_r ( mixed $expression [, array $ignore] )
Use the $ignore parameter to provide an array of property names that shouldn't be followed recursively.
function u_print_r($subject, $ignore = array(), $depth = 1, $refChain = array())
{
if ($depth > 20) return;
if (is_object($subject)) {
foreach ($refChain as $refVal)
if ($refVal === $subject) {
echo "*RECURSION*\n";
return;
}
array_push($refChain, $subject);
echo get_class($subject) . " Object ( \n";
$subject = (array) $subject;
foreach ($subject as $key => $val)
if (is_array($ignore) && !in_array($key, $ignore, 1)) {
echo str_repeat(" ", $depth * 4) . '[';
if ($key{0} == "\0") {
$keyParts = explode("\0", $key);
echo $keyParts[2] . (($keyParts[1] == '*') ? ':protected' : ':private');
} else
echo $key;
echo '] => ';
u_print_r($val, $ignore, $depth + 1, $refChain);
}
echo str_repeat(" ", ($depth - 1) * 4) . ")\n";
array_pop($refChain);
} elseif (is_array($subject)) {
echo "Array ( \n";
foreach ($subject as $key => $val)
if (is_array($ignore) && !in_array($key, $ignore, 1)) {
echo str_repeat(" ", $depth * 4) . '[' . $key . '] => ';
u_print_r($val, $ignore, $depth + 1, $refChain);
}
echo str_repeat(" ", ($depth - 1) * 4) . ")\n";
} else
echo $subject . "\n";
}
?>
Example:
class test {
public $var1 = 'a';
protected $var2 = 'b';
private $var3 = 'c';
protected $array = array('x', 'y', 'z');
}
$test = new test();
$test->recursiveRef = $test;
$test->anotherRecursiveRef->recursiveRef = $test;
$test->dont->follow = 'me';
void u_print_r ( mixed $expression [, array $ignore] )0
?>
void u_print_r ( mixed $expression [, array $ignore] )2
void u_print_r ( mixed $expression [, array $ignore] )3