one preg_replace

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

DoppyNL

Postby DoppyNL » Sat Aug 10, 2002 10:53 pm

I don't know what you exactly want to do, but perhaps you could use this:

Code: Select all

str_replace('\r\n','<br>',$subject_value);

this will simply change a complete string.

as far as I know "\r\n" works normally in an textarea, so you won't have to replace those when creating a form.
You will only need to replace them when you're placing the code outside input fields in you're html-page. But than I allways manage with str_replace.

Hope this helps

Greetz Daan

Jay

Postby Jay » Sat Aug 10, 2002 11:04 pm

To display a new line in a text area you need to use the html code for a new line character which is &#10;. \r\n will only work in scripting languages, like PHP and Javascript because they can recognise spaces. HTML truncates spaces (except if you use the <pre></pre> tags which does not format data at all, perfect for viewing arrays with the print_r() function neatly)

By the way, it's advisable not to use regular expression functions for such basic tasks as converting set characters without pattern matching, because it takes up server strain. The str_replace() function is far more effective at this :)

User avatar
Oleg Butuzov
Last Samuray
Last Samuray
Posts: 831
Joined: Sun Jun 02, 2002 3:09 am

Postby Oleg Butuzov » Sun Aug 11, 2002 1:23 am

or you can do...

echo "<textarea>";
nl2br($var);
echo "</textarea>";

Jay

Postby Jay » Mon Aug 12, 2002 2:19 am

Pejone wrote:or you can do...

echo "<textarea>";
nl2br($var);
echo "</textarea>";

But that won't work because the textarea displays <br> as <br> and not new lines. You have to use &#10; for a new line in a text area


Return to “PHP coding => General”

Who is online

Users browsing this forum: Exabot [Bot], LeonelDoctor and 1 guest

cron