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: Thanks Tony for the share!
  • Tony Stark: Hi Admin, Thanks for the tutorial. Chicken is too slow so I Google for an alternative and I found...
  • admin: How about installing Gen-B instead of Gen-C? Here’s the download link for it:...
  • aznspryd: I hacked my PSP-3000 and installed Gen-C, but everytime i try opening a game, it restarts the psp
  • vinoth: Hi ,bro its realy workin well i.E thanks alot.