php - Detecting beginning of line and end of line in preg_replace -
$data = $_post['data']; echo '<form method="post"><textarea name="data"></textarea><input type="submit">'; echo preg_replace( "#/*[?+]/n#", "[h1]$1[/h1]",$str ); is possible preg_replace detect beginning of line , apply html code entire line based on few characters @ beginning of it?
here, based on * if data were:
*this row 1 //output <h1>this row one</h1> **this row 2 //output <h2>this row two</h2> ***this row 3 //output <h3>this row three</h3> ***this row * 3 //output <h3>this row * three</h3> row * 3 //output row * 3 i'm having troble detecting begining of line , end of line , wrapping tags around text in line.
i don't need * in between line matched. failed effors included. can help?
see http://www.regular-expressions.info/anchors.html full details
// replace *** h3 $str = preg_replace('/^\*{3}([^\*].*)$/', '<h3>$1</h3>', $str); // replace ** h2 $str = preg_replace('/^\*{2}([^\*].*)$/', '<h2>$1</h2>', $str); // replace * h1 $str = preg_replace('/^\*([^\*].*)$/', '<h1>$1</h1>', $str); explanation: * special character in regular expressions, has escaped, so: \*.
^\* searches * @ beginning of string. [^\*] searches not * -- square brackets used search class of characters, , ^ means 'not'. therefore, search *** without matching ****, use expression /^\*{3}[^\*]/.
Comments
Post a Comment