Search Keyword:

01Dec

trim() and nl2br() PHP function in Javascript

Posted by admin as Javascript/AJAX

These functions are equivalent to PHP’s trim() and nl2br() function written in Javascript and are most useful in working with formatting text inputs and textareas values.Trim() strips whitespaces from the beginning and end of a string and nl2br() inserts HTML line breaks (<br />) and replaces newlines or carriage returns(Enter key) in a string.

The function:

function trimStr(input_string)
{
return input_string.replace(/^\s+|\s+$/g, ”);
}

function nl2brStr(input_string)
{
return input_string.replace(/(\r\n|\r|\n)/g, “<br />”);
}

Usage:

You can use the functions individually or by joining them together.See the demo for sample implementations by clicking here.

  • Digg
  • del.icio.us
  • Tumblr
  • StumbleUpon
  • Technorati
  • Reddit
  • NewsVine
  • Slashdot
  • DZone
  • Facebook
  • Twitter

Comment Form

Sponsored Links



Recent Comments

  • admin: Hello Josh, Personally, my PSP is a PSP 3002 with OFW 5.03. Follow the steps and you will be fine. If ever...
  • josh: ok im a 100% noob and i have just brought myself a psp 3002 with ofw 5.03 ive been researching alot about...
  • admin: You cannot downgrade it to 5.03. Unfortunately, you have to wait for the new hack for versions above 5.03,...
  • kev: Hello, what can i do if my psp version is higher than 5.03 ? How do i downgrade it ?
  • admin: Thanks Silver for your solution and suggestion! Very much appreciated. Thanks!