Skip to content

Instantly share code, notes, and snippets.

@janogarcia
Created December 16, 2010 09:03
Show Gist options
  • Save janogarcia/743209 to your computer and use it in GitHub Desktop.
Save janogarcia/743209 to your computer and use it in GitHub Desktop.
PHP port of JavaScript String slice() method
<?php
/**
* PHP port of JavaScript String slice() method
*
* @param string $str
* @param int $start
* @param int $end (optional)
*/
function str_slice($str, $start, $end = FALSE)
{
$max = strlen($str);
$start = ($start<0)? $max+$start : $start;
$end = ($end<0)? $max+$end : (($end === FALSE)? $max : $end);
$slice = substr($str, $start, ($end>$start)? $end-$start : 0);
return ($slice === FALSE)? '' : $slice;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment