Showing posts with label PHP functions in Javascript. Show all posts
Showing posts with label PHP functions in Javascript. Show all posts

Friday, 29 April 2011

Javascript using PHP functions

Ever wanted to use functions easily available in the PHP library but with Javascript?

I found a new project called the PHP-JS project and it's pretty cool. They have covered most of the PHP functions and written some really good Javascript. The other day I needed a snippet of Javascript code to Uppercase the first letters of new words, which is the ucwords() function in PHP. Here's their code:

function ucwords (str) {
    return (str + '').replace(/^([a-z])|\s+([a-z])/g, function ($1) {
        return $1.toUpperCase();
    });
}

So to use it I write:

//Capitalizes the words to 'The Cat Sat On The Mat'
alert( ucwords('the cat sat on the mat') );

Nice. Visit the site at http://www.phpjs.org/.