Jump to content

Changing One String Into Another


Grabeorama

Recommended Posts

$userinput is what the user wrote.

<?php$userinput = str_replace("\n", "<br />", $userinput);?>

So if the user inputs:My name is Bob.\nMy last name is Smith.$userinput will become:My name is Bob.<br />My last name is Smith.

Link to comment
Share on other sites

Or just use the nl2br() function:
$userInput = nl2br($userInput);

I wouldn't use that just because it's a very limited function that does what I mention.However, there is nothing wrong with it.
Link to comment
Share on other sites

I wouldn't use that just because it's a very limited function that does what I mention.However, there is nothing wrong with it.
nl2br() may be the more limited of the two functions, but the advantage of it is that it is faster - much faster. In my benchmarking test, it ran over twice as fast as the str_replace() call. See for yourself:
<?php	header("content-type:text/plain");	$nl2br = array();	$str_replace = array();	for ($j = 1; $j <= 20; $j++) {		$then = microtime(true);		for ($i = 1; $i <= 100000; $i++) nl2br("\n");		$nl2br[] = microtime(true) - $then;		$then = microtime(true);		for ($i = 1; $i <= 100000; $i++) str_replace("\n", "<br />", "\n");		$str_replace[] = microtime(true) - $then;	}	$nl2br = array_sum($nl2br) / count($nl2br);	$str_replace = array_sum($str_replace) / count($str_replace);	echo "Speeds (in seconds)\n";	echo "nl2br():\t\t$nl2br\n";	echo "str_replace():\t\t$str_replace\n";	echo "Difference:\t\t" . ($nl2br - $str_replace) . "\n";	echo "Ratio:\t\t\t" . ($nl2br / $str_replace);?>

Link to comment
Share on other sites

nl2br() may be the more limited of the two functions, but the advantage of it is that it is faster - much faster. In my benchmarking test, it ran over twice as fast as the str_replace() call. See for yourself:
<?php	header("content-type:text/plain");	$nl2br = array();	$str_replace = array();	for ($j = 1; $j <= 20; $j++) {		$then = microtime(true);		for ($i = 1; $i <= 100000; $i++) nl2br("\n");		$nl2br[] = microtime(true) - $then;		$then = microtime(true);		for ($i = 1; $i <= 100000; $i++) str_replace("\n", "<br />", "\n");		$str_replace[] = microtime(true) - $then;	}	$nl2br = array_sum($nl2br) / count($nl2br);	$str_replace = array_sum($str_replace) / count($str_replace);	echo "Speeds (in seconds)\n";	echo "nl2br():\t\t$nl2br\n";	echo "str_replace():\t\t$str_replace\n";	echo "Difference:\t\t" . ($nl2br - $str_replace) . "\n";	echo "Ratio:\t\t\t" . ($nl2br / $str_replace);?>

The problem is nl2br() returns pure HTML, so you must be careful in which order you transform the string.That's why I tell people to use str_replace(), but hey, if you're not getting errors and have no problem concering limits use nl2br() by all means.Oh and if you're using an HTML file as opposed to an XHTML file use nl2br($string, false); for the sake of file space.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...