Skip to content
Advertisement

PHP – replace all “n” Occurrences in a string

I am getting a string in my PHP output with lots of n characters which I would like to convert into <p> to get a new line characters in html outpot.

I have tried following:

strtr($myString, "n", "<p>");    
string_replace ("n", "<p>", $myString);

Both of these doesn’t seems to work. Any ideas what’s wrong in the syntax?

Advertisement

Answer

Try this:

PHP:

<?php

$myString = 'HellonWorld';

echo '<p>'. implode('</p><p>', explode('n', $myString)) .'</p>';

?>

Output:

<p>Hello</p><p>World</p>
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement