PHP JS Style String Functions.
Posted March 16th 2012, 12:15pm
If you've moved on to learning PHP after Javascript, it may take sometime to get used to the new string manipulation functions.

This topic will provide you with some very simple PHP function which will allow you to manipulate strings, using the same function names as Javascript.
Allowing you to keep those familiar alias's.
Such as [ic]indexOf(), charAt(), substring(), split()[/ic]

The first parameter is always the string on which to perform the function.

function substring($str,$start,$end = false)
if(!$end) $end = strlen($str);
return substr($str,$start, $end - $start);
function charAt($str,$index)
return substr($str,$index,1);
function lastIndexOf($str,$search,$start = 0)
return strripos($str,$search,$start);
function indexOf($str,$search,$start = 0)
return strpos($str,$search,$start);

As you can see these functions are tiny and in the bigger picture, a little pointless. But if you familiar with JS, this will provide a more familiar way of manipulting strings.
Posts: 509
Joined: July 9th 2011, 11:00pm
Location: England
Likes Given: 15
Likes Received: 26

Who is online

Users browsing this forum: No registered users and 0 guests