substr

(PHP 4, PHP 5, PHP 7)

substrReturn part of a string

Description

string substr ( string $string , int $start [, int $length ] )

Returns the portion of string specified by the start and length parameters.

Parameters

string

The input string. Must be one character or longer.

start

If start is non-negative, the returned string will start at the start'th position in string, counting from zero. For instance, in the string 'abcdef', the character at position 0 is 'a', the character at position 2 is 'c', and so forth.

If start is negative, the returned string will start at the start'th character from the end of string.

If string is less than start characters long, FALSE will be returned.

Example #1 Using a negative start

<?php
$rest 
substr("abcdef", -1);    // returns "f"
$rest substr("abcdef", -2);    // returns "ef"
$rest substr("abcdef", -31); // returns "d"
?>

length

If length is given and is positive, the string returned will contain at most length characters beginning from start (depending on the length of string).

If length is given and is negative, then that many characters will be omitted from the end of string (after the start position has been calculated when a start is negative). If start denotes the position of this truncation or beyond, FALSE will be returned.

If length is given and is 0, FALSE or NULL, an empty string will be returned.

If length is omitted, the substring starting from start until the end of the string will be returned.

Example #2 Using a negative length

<?php
$rest 
substr("abcdef"0, -1);  // returns "abcde"
$rest substr("abcdef"2, -1);  // returns "cde"
$rest substr("abcdef"4, -4);  // returns false
$rest substr("abcdef", -3, -1); // returns "de"
?>

Return Values

Returns the extracted part of string; or FALSE on failure, or an empty string.

Changelog

Version Description
7.0.0 If string is equal to start characters long, an empty string will be returned. Prior to this version, FALSE was returned in this case.
5.2.2 - 5.2.6 If the start parameter indicates the position of a negative truncation or beyond, false is returned. Other versions get the string from start.

Examples

Example #3 Basic substr() usage

<?php
echo substr('abcdef'1);     // bcdef
echo substr('abcdef'13);  // bcd
echo substr('abcdef'04);  // abcd
echo substr('abcdef'08);  // abcdef
echo substr('abcdef', -11); // f

// Accessing single characters in a string
// can also be achieved using "square brackets"
$string 'abcdef';
echo 
$string[0];                 // a
echo $string[3];                 // d
echo $string[strlen($string)-1]; // f

?>

Example #4 substr() casting behaviour

<?php
class apple {
    public function 
__toString() {
        return 
"green";
    }
}

echo 
"1) ".var_export(substr("pear"02), true).PHP_EOL;
echo 
"2) ".var_export(substr(5432102), true).PHP_EOL;
echo 
"3) ".var_export(substr(new apple(), 02), true).PHP_EOL;
echo 
"4) ".var_export(substr(true01), true).PHP_EOL;
echo 
"5) ".var_export(substr(false01), true).PHP_EOL;
echo 
"6) ".var_export(substr(""01), true).PHP_EOL;
echo 
"7) ".var_export(substr(1.2e304), true).PHP_EOL;
?>

Output of the above example in PHP 7:

1) 'pe'
2) '54'
3) 'gr'
4) '1'
5) ''
6) ''
7) '1200'

Output of the above example in PHP 5:

1) 'pe'
2) '54'
3) 'gr'
4) '1'
5) false
6) false
7) '1200'

Errors/Exceptions

Returns FALSE on error.

<?php
var_dump
(substr('a'2)); // bool(false)
?>

See Also

add a note add a note

User Contributed Notes 49 notes

up
104
Andreas Bur (andreas dot buro at gmail dot com)
8 years ago
For getting a substring of UTF-8 characters, I highly recommend mb_substr

<?php
        $utf8string
= "cakeæøå";

        echo
substr($utf8string,0,5);
       
// output cake#
       
echo mb_substr($utf8string,0,5,'UTF-8');
       
//output cakeæ
?>
up
71
biohazard dot ge at gmail dot com
4 years ago
may be by following functions will be easier to extract the needed sub parts from a string:

<?php
after
('@', 'biohazard@online.ge');
//returns 'online.ge'
//from the first occurrence of '@'

before ('@', 'biohazard@online.ge');
//returns 'biohazard'
//from the first occurrence of '@'

between ('@', '.', 'biohazard@online.ge');
//returns 'online'
//from the first occurrence of '@'

after_last ('[', 'sin[90]*cos[180]');
//returns '180]'
//from the last occurrence of '['

before_last ('[', 'sin[90]*cos[180]');
//returns 'sin[90]*cos['
//from the last occurrence of '['

between_last ('[', ']', 'sin[90]*cos[180]');
//returns '180'
//from the last occurrence of '['
?>

here comes the source:

<?php

   
function after ($this, $inthat)
    {
        if (!
is_bool(strpos($inthat, $this)))
        return
substr($inthat, strpos($inthat,$this)+strlen($this));
    };

    function
after_last ($this, $inthat)
    {
        if (!
is_bool(strrevpos($inthat, $this)))
        return
substr($inthat, strrevpos($inthat, $this)+strlen($this));
    };

    function
before ($this, $inthat)
    {
        return
substr($inthat, 0, strpos($inthat, $this));
    };

    function
before_last ($this, $inthat)
    {
        return
substr($inthat, 0, strrevpos($inthat, $this));
    };

    function
between ($this, $that, $inthat)
    {
        return
before ($that, after($this, $inthat));
    };

    function
between_last ($this, $that, $inthat)
    {
     return
after_last($this, before_last($that, $inthat));
    };

// use strrevpos function in case your php version does not include it
function strrevpos($instr, $needle)
{
   
$rev_pos = strpos (strrev($instr), strrev($needle));
    if (
$rev_pos===false) return false;
    else return
strlen($instr) - $rev_pos - strlen($needle);
};
?>
up
3
php at richardneill dot org
3 years ago
Frequently we want to check whether one string ends with (or starts with) another.  These may be useful:

function str_ends($string,$end){
    return (substr($string,-strlen($end),strlen($end)) === $end);
}   
function str_begins($string,$start){
    return (substr($string,0,strlen($start)) === $start);
}   

Example:
if (str_ends ($filename, ".jpg")){
   echo "The extension is .jpg";
}
up
14
pugazhenthi k
4 years ago
<?Php

### SUB STRING  BY WORD USING substr() and strpos()  #####

### THIS SCRIPT WILL RETURN PART OF STRING  WITHOUT WORD BREAK ###

$description = ‘your description here your description here your description here your description here your description here your description here your description hereyour description here your description here’  // your description here .

$no_letter = 30 ;

if(
strlen($desctiption) > 30 )
{
     echo
substr($description,0,strpos($description,’ ‘,30));             //strpos to find ‘ ‘ after 30 characters.
}
else {
     echo
$description;
}

?>
up
3
kriskra at gmail dot com
9 years ago
The javascript charAt equivalent in php of felipe has a little bug. It's necessary to compare the type (implicit) aswell or the function returns a wrong result:
<?php
function charAt($str,$pos) {
    return (
substr($str,$pos,1) !== false) ? substr($str,$pos,1) : -1;
}
?>
up
9
Petez
10 years ago
I wanted to work out the fastest way to get the first few characters from a string, so I ran the following experiment to compare substr, direct string access and strstr:

<?php
/* substr access */
beginTimer();
for (
$i = 0; $i < 1500000; $i++){
   
$opening = substr($string,0,11);
    if (
$opening == 'Lorem ipsum'){
       
true;
    }else{
       
false;
    }
}
$endtime1 = endTimer();

/* direct access */
beginTimer();
for (
$i = 0; $i < 1500000; $i++){
    if (
$string[0] == 'L' && $string[1] == 'o' && $string[2] == 'r' && $string[3] == 'e' && $string[4] == 'm' && $string[5] == ' ' && $string[6] == 'i' && $string[7] == 'p' && $string[8] == 's' && $string[9] == 'u' && $string[10] == 'm'){
       
true;
    }else{
       
false;
    }
}
$endtime2 = endTimer();

/* strstr access */
beginTimer();
for (
$i = 0; $i < 1500000; $i++){
   
$opening = strstr($string,'Lorem ipsum');
    if (
$opening == true){
       
true;
    }else{
       
false;
    }
}
$endtime3 = endTimer();

echo
$endtime1."\r\n".$endtime2."\r\n".$endtime3;
?>

The string was 6 paragraphs of Lorem Ipsum, and I was trying match the first two words. The experiment was run 3 times and averaged. The results were:

(substr) 3.24
(direct access) 11.49
(strstr) 4.96

(With standard deviations 0.01, 0.02 and 0.04)

THEREFORE substr is the fastest of the three methods for getting the first few letters of a string.
up
11
mar dot czapla at gmail dot com
9 years ago
Here we have gr8 function which simply convert ip address to a number using substr with negative offset.
You can need it if you want to compare some IP addresses converted to a numbers.
For example when using ip2country, or eliminating same range of ip addresses from your website :D

<?php

function ip2no($val)
{   
    list(
$A,$B,$C,$D)    =    explode(".",$val);
    return
       
substr("000".$A,-3).
       
substr("000".$B,-3).
       
substr("000".$C,-3).
       
substr("000".$D,-3);
}

$min        =    ip2no("10.11.1.0");
$max        =    ip2no("111.11.1.0");
$visitor    =    ip2no("105.1.20.200");

if(
$min<$visitor && $visitor<$max)   
    {    echo
'Welcome !';    }
else   
    {    echo
'Get out of here !';    }

?>
up
4
leon weidauer
6 years ago
When using a value of a wrong type as second parameter , substr() does not return FALSE but NULL although the docs say, it should return FALSE on error.

Prior to PHP 5.3, substr() tries to cast the second parameter to int and doesn't throw any errors. Since PHP 5.3 a warning is thrown.
up
6
webmaster at oehoeboeroe dot nl
8 years ago
You might expect substr('123456', 6) to return an empty string. Instead it returns boolean FALSE.

This behavior should be mentioned in the Return Values section of the manual. Instead it is only mentioned in the Parameters section.

If you need an empty string instead of a boolean FALSE you should typecast the result to a string.

<?php
$a
= substr('123456', 6);              // equivalent to $a = FALSE
$a = (string) substr('123456', 6);   // equivalent to $a = '';
?>
up
10
fanfatal at fanfatal dot pl
12 years ago
Hmm ... this is a script I wrote, whitch is very similar to substr, but it isn't takes html and bbcode for counting and it takes portion of string and show avoided (html & bbcode) tags too ;]
Specially usefull for show part of serach result included html and bbcode tags

<?php

/**
* string csubstr ( string string, int start [, int length] )
*
* @author FanFataL
* @param string string
* @param int start
* @param [int length]
* @return string
*/
function csubstr($string, $start, $length=false) {
   
$pattern = '/(\[\w+[^\]]*?\]|\[\/\w+\]|<\w+[^>]*?>|<\/\w+>)/i';
   
$clean = preg_replace($pattern, chr(1), $string);
    if(!
$length)
       
$str = substr($clean, $start);
    else {
       
$str = substr($clean, $start, $length);
       
$str = substr($clean, $start, $length + substr_count($str, chr(1)));
    }
   
$pattern = str_replace(chr(1),'(.*?)',preg_quote($str));
    if(
preg_match('/'.$pattern.'/is', $string, $matched))
        return
$matched[0];
    return
$string;
}

?>

Using this is similar to simple substr.

Greatings ;]
...
up
4
greg at apparel dot com
3 years ago
Coming to PHP from classic ASP I am used to the Left() and Right() functions built into ASP so I did a quick PHPversion. hope these help someone else making the switch

function left($str, $length) {
    return substr($str, 0, $length);
}

function right($str, $length) {
    return substr($str, -$length);
}
up
10
slow at acedsl dot com
6 years ago
Anyone coming from the Python world will be accustomed to making substrings by using a "slice index" on a string.  The following function emulates basic Python string slice behavior. (A more elaborate version could be made to support array input as well as string, and the optional third "step" argument.)

<?php

function py_slice($input, $slice) {
   
$arg = explode(':', $slice);
   
$start = intval($arg[0]);
    if (
$start < 0) {
       
$start += strlen($input);
    }
    if (
count($arg) === 1) {
        return
substr($input, $start, 1);
    }
    if (
trim($arg[1]) === '') {
        return
substr($input, $start);
    }
   
$end = intval($arg[1]);
    if (
$end < 0) {
       
$end += strlen($input);
    }
    return
substr($input, $start, $end - $start);
}

print
py_slice('abcdefg', '2') . "\n";
print
py_slice('abcdefg', '2:4') . "\n";
print
py_slice('abcdefg', '2:') . "\n";
print
py_slice('abcdefg', ':4') . "\n";
print
py_slice('abcdefg', ':-3') . "\n";
print
py_slice('abcdefg', '-3:') . "\n";

?>

The $slice parameter can be a single character index, or a range separated by a colon. The start of the range is inclusive and the end is exclusive, which may be  counterintuitive. (Eg, py_slice('abcdefg', '2:4') yields 'cd' not 'cde'). A negative range value means to count from the end of the string instead of the beginning. Both the start and end of the range may be omitted; the start defaults to 0 and the end defaults to the total length of the input.

The output from the examples:
c
cd
cdefg
abcd
abcd
efg
up
4
woutermb at gmail dot com
12 years ago
Well this is a script I wrote, what it does is chop up long words with malicious meaning into several parts. This way, a chat in a table will not get stretched anymore.

<?php

function text($string,$limit=20,$chop=10){

$text = explode(" ",$string);
while(list(
$key, $value) = each($text)){
   
$length = strlen($value);
    if(
$length >=20){
        for(
$i=0;$i<=$length;$i+=10){
           
$new .= substr($value, $i, 10);
           
$new .= " ";
        }
        
$post .= $new;
    }
    elseif(
$length <=15){
       
$post .= $value;
    }
   
$post .= " ";
}
return(
$post);
}

// for example, this would return:
$output = text("Well this text doesn't get cut up, yet thisssssssssssssssssssssssss one does.", 10, 5);

echo(
$output); // "Well this text doesn't get cup up, yet thiss sssss sssss sssss sssss sss one does."
?>

I hope it was useful.. :)
up
4
Bradley from California
11 years ago
Add on to (a function originally written by) "Matias from Argentina": str_format_number function.

Just added handling of $String shorter then $Format by adding a side to start the fill and a string length to the while loop.

<?php
function str_format_number($String, $Format, $Start = 'left'){
   
//If we want to fill from right to left incase string is shorter then format
   
if ($Start == 'right') {
       
$String = strrev($String);
       
$Format = strrev($Format);
    }
    if(
$Format == '') return $String;
    if(
$String == '') return $String;   
   
$Result = '';
   
$FormatPos = 0;
   
$StringPos = 0;
    while ((
strlen($Format) - 1) >= $FormatPos && strlen($String) > $StringPos) {
       
//If its a number => stores it
       
if (is_numeric(substr($Format, $FormatPos, 1))) {
           
$Result .= substr($String, $StringPos, 1);
           
$StringPos++;
           
//If it is not a number => stores the caracter
       
} else {
           
$Result .= substr($Format, $FormatPos, 1);
        }
       
//Next caracter at the mask.
       
$FormatPos++;
    }
    if (
$Start == 'right') $Result = strrev($Result);
    return
$Result;
}
?>
up
2
link
8 years ago
I created some functions for entity-safe splitting+lengthcounting:

<?php
function strlen_entities($text)
{
   
preg_match_all(
       
'/((?:&(?:#[0-9]{2,}|[a-z]{2,});)|(?:[^&])|'.        
       
'(?:&(?!\w;)))s',$text,$textarray);
    return
count($textarray[0]);

function
substr_entities($text,$start,$limit=0)
{
   
$return = '';
   
preg_match_all(
       
'/((?:&(?:#[0-9]{2,}|[a-z]{2,});)|(?:[^&])|'.        
       
'(?:&(?!\w;)))s',$text,$textarray);
   
$textarray = $textarray[0];
   
$numchars = count($textarray)-1;
    if (
$start>=$numchars)
        return
false;
    if (
$start<0)
    {
       
$start = ($numchars)+$start+1;
    }
    if (
$start>=0)
    {
        if (
$limit==0)
        {
           
$end=$numchars;
        }
        elseif (
$limit>0)
        {
           
$end = $start+($limit-1);
        }
        else
        {
           
$end = ($numchars)+$limit;
        }

        for (
$i=$start;$i<=$end;$i++)
        {
           
$return .= $textarray[$i];
        }
        return
$return;
    }
}
?>
up
2
Nadeem
3 years ago
Truncate a float number. Similar to the Excel trunc function.

<?php
function truncate_number($val,$decimals=2){
   
       
$number=array();
       
$number=explode(".",$val);           
       
$result=0;

        if (
count($number)>1){

           
$result = $number[0] . "." . substr($number[1],0,$decimals); 
           
        } else {
       
           
$result = $val;
           
        }               

        unset(
$number);

        return
$result;   
}

echo
truncate_number(99.123456,2); //result = 99.12
echo truncate_number(99.123456,5); //result = 99.12345
echo truncate_number(99.123456,1); //result = 99.1
?>
up
1
feedback at realitymedias dot com
11 years ago
This function can replace substr() in some situations you don't want to cut right in the middle of a word. strtrim will cut between words when it is possible choosing the closest possible final string len to return. the maxoverflow parameter lets you choose how many characters can overflow past the maxlen parameter.

<?php

function strtrim($str, $maxlen=100, $elli=NULL, $maxoverflow=15) {
    global
$CONF;
       
    if (
strlen($str) > $maxlen) {
           
        if (
$CONF["BODY_TRIM_METHOD_STRLEN"]) {
            return
substr($str, 0, $maxlen);
        }
           
       
$output = NULL;
       
$body = explode(" ", $str);
       
$body_count = count($body);
       
       
$i=0;
   
        do {
           
$output .= $body[$i]." ";
           
$thisLen = strlen($output);
           
$cycle = ($thisLen < $maxlen && $i < $body_count-1 && ($thisLen+strlen($body[$i+1])) < $maxlen+$maxoverflow?true:false);
           
$i++;
        } while (
$cycle);
        return
$output.$elli;
    }
    else return
$str;
}

?>
up
2
egingell at sisna dot com
11 years ago
<?php

/**
* string substrpos(string $str, mixed $start [[, mixed $end], boolean $ignore_case])
*
* If $start is a string, substrpos will return the string from the position of the first occuring $start to $end
*
* If $end is a string, substrpos will return the string from $start to the position of the first occuring $end
*
* If the first character in (string) $start or (string) $end is '-', the last occuring string will be used.
*
* If $ignore_case is true, substrpos will not care about the case.
* If $ignore_case is false (or anything that is not (boolean) true, the function will be case sensitive.
*        Both of the above: only applies if either $start or $end are strings.
*
* echo substrpos('This is a string with 0123456789 numbers in it.', 5, '5');
*        // Prints 'is a string with 01234';
*
* echo substrpos('This is a string with 0123456789 numbers in it.', '5', 5);
*        // Prints '56789'
*
* echo substrpos('This is a string with 0123456789 numbers in it and two strings.', -60, '-string')
*        // Prints 's is a string with 0123456789 numbers in it and two '
*
* echo substrpos('This is a string with 0123456789 numbers in it and two strings.', -60, '-STRING', true)
*        // Prints 's is a string with 0123456789 numbers in it and two '
*
* echo substrpos('This is a string with 0123456789 numbers in it and two strings.', -60, '-STRING', false)
*        // Prints 's is a string with 0123456789 numbers in it and two strings.'
*
* Warnings:
*        Since $start and $end both take either a string or an integer:
*            If the character or string you are searching $str for is a number, pass it as a quoted string.
*        If $end is (integer) 0, an empty string will be returned.
*        Since this function takes negative strings ('-search_string'):
*            If the string your using in $start or $end is a '-' or begins with a '-' escape it with a '\'.
*            This only applies to the *first* character of $start or $end.
*/

// Define stripos() if not defined (PHP < 5).
if (!is_callable("stripos")) {
    function
stripos($str, $needle, $offset = 0) {
        return
strpos(strtolower($str), strtolower($needle), $offset);
    }
}

function
substrpos($str, $start, $end = false, $ignore_case = false) {
   
// Use variable functions
   
if ($ignore_case === true) {
       
$strpos = 'stripos'; // stripos() is included above in case it's not defined (PHP < 5).
   
} else {
       
$strpos = 'strpos';
    }

   
// If end is false, set it to the length of $str
   
if ($end === false) {
       
$end = strlen($str);
    }

   
// If $start is a string do what's needed to make it an integer position for substr().
   
if (is_string($start)) {
       
// If $start begins with '-' start processing until there's no more matches and use the last one found.
       
if ($start{0} == '-') {
           
// Strip off the '-'
           
$start = substr($start, 1);
           
$found = false;
           
$pos = 0;
            while((
$curr_pos = $strpos($str, $start, $pos)) !== false) {
               
$found = true;
               
$pos = $curr_pos + 1;
            }
            if (
$found === false) {
               
$pos = false;
            } else {
               
$pos -= 1;
            }
        } else {
           
// If $start begins with '\-', strip off the '\'.
           
if ($start{0} . $start{1} == '\-') {
               
$start = substr($start, 1);
            }
           
$pos = $strpos($str, $start);
        }
       
$start = $pos !== false ? $pos : 0;
    }

   
// Chop the string from $start to strlen($str).
   
$str = substr($str, $start);

   
// If $end is a string, do exactly what was done to $start, above.
   
if (is_string($end)) {
        if (
$end{0} == '-') {
           
$end = substr($end, 1);
           
$found = false;
           
$pos = 0;
            while((
$curr_pos = strpos($str, $end, $pos)) !== false) {
               
$found = true;
               
$pos = $curr_pos + 1;
            }
            if (
$found === false) {
               
$pos = false;
            } else {
               
$pos -= 1;
            }
        } else {
            if (
$end{0} . $end{1} == '\-') {
               
$end = substr($end, 1);
            }
           
$pos = $strpos($str, $end);
        }
       
$end = $pos !== false ? $pos : strlen($str);
    }

   
// Since $str has already been chopped at $start, we can pass 0 as the new $start for substr()
   
return substr($str, 0, $end);
}

?>
up
4
frank at jkelloggs dot dk
12 years ago
Regarding the utf8_substr function from lmak: The pattern '/./u' doesn't match newline characters. This means that the substring from 0 to the total length of the string will miss the number of characters in the end matching the number of newlines in the string. To fix this one can add the s modifier (PCRE_DOTALL) in the pattern:

<?php
function utf8_substr($str,$start)
{
  
preg_match_all("/./su", $str, $ar);

   if(
func_num_args() >= 3) {
      
$end = func_get_arg(2);
       return
join("",array_slice($ar[0],$start,$end));
   } else {
       return
join("",array_slice($ar[0],$start));
   }
}
?>
up
2
ivanhoe011 at gmail dot com
12 years ago
If you need just a single character from the string you don't need to use substr(), just use curly braces notation:

<?php
   
// both lines will output the 3rd character
   
echo substr($my_string, 2, 1);
    echo
$my_string{2}; 
?>

curly braces syntax is faster and more readable IMHO..
up
3
vnonov at gmail dot com / Viktor Nonov
7 years ago
<?php

//removes string from the end of other

function removeFromEnd($string, $stringToRemove) {
   
$stringToRemoveLen = strlen($stringToRemove);
   
$stringLen = strlen($string);
   
   
$pos = $stringLen - $stringToRemoveLen;

   
$out = substr($string, 0, $pos);

    return
$out;
}

$string = 'picture.jpg.jpg';
$string = removeFromEnd($string, '.jpg');
?>
up
1
post [at] jannik - zappe [dot] de
9 years ago
Just a little function to cut a string by the wanted amount. Works in both directions.

<?php
function cutString($str, $amount = 1, $dir = "right")
{
  if((
$n = strlen($str)) > 0)
  {
    if(
$dir == "right")
    {
     
$start = 0;
     
$end = $n-$amount;
    } elseif(
$dir == "left") {
     
$start = $amount;
     
$end = $n;
    }
   
    return
substr($str, $start, $end);
  } else return
false;
}
?>

Enjoy ;)
up
2
Cristianlf
7 years ago
I needed a function like lpad from oracle, or right from SQL
then I use this code :

<?php
function right($string,$chars)
{
   
$vright = substr($string, strlen($string)-$chars,$chars);
    return
$vright;
   
}

    echo
right('0r0j4152',4);
?>

Result:
4152
------------------------------------------------
This function is really simple, I just wanted to share, maybe helps someone out there. 

regards,
up
1
nikolai dot wuestemann at t-online dot de
6 years ago
If you want to have a string BETWEEN two strings, just use this function:

<?php
function get_between($input, $start, $end)
{
 
$substr = substr($input, strlen($start)+strpos($input, $start), (strlen($input) - strpos($input, $end))*(-1));
  return
$substr;
}

//Example:

$string = "123456789";
$a = "12";
$b = "9";

echo
get_between($string, $a, $b);

//Output:
//345678
?>
up
2
steve at unicycle dot co dot nz
12 years ago
To quickly trim an optional trailing slash off the end of a path name:

if (substr( $path, -1 ) == '/') $path = substr( $path, 0, -1 );
up
2
rob NOSPAM at clancentric dot net
12 years ago
I have developed a function with a similar outcome to jay's

Checks if the last character is or isnt a space. (does it the normal way if it is)
It explodes the string into an array of seperate works, the effect is... it chops off anything after and including the last space.

<?php
function limit_string($string, $charlimit)
{
    if(
substr($string,$charlimit-1,1) != ' ')
    {
       
$string = substr($string,'0',$charlimit);
       
$array = explode(' ',$string);
       
array_pop($array);
       
$new_string = implode(' ',$array);

        return
$new_string.'...';
    }
    else
    {   
        return
substr($string,'0',$charlimit-1).'...';
    }
}
?>
up
1
fatihmertdogancan at hotmail dot com
3 years ago
[English]
I created python similar accesing list or string with php substr & strrev functions.

Use: str($string,$pattern)

About the python pattern,
http://docs.python.org/release/1.5.1p1/tut/strings.html
http://effbot.org/zone/python-list.htm

About of pattern structures
[start:stop:step]

Example,
<?php
$s
= "fatihmertdogancan";
echo
str($s,"1:9:-2");
echo
"<br/>";
echo
str($s,"1:-3:-2");
echo
"<br/>";
echo
str($s,"1:-11:-5");
echo
"<br/>";
echo
str($s,"1:9:4");
?>

Output,
thetoacn
eht
aom
htan

This is function phpfiddle link: http://phpfiddle.org/main/code/e82-y5d

or source;

<?php
   
function str($str,$pattern){
       
//[start:stop:step]
        //pattern ->            ([-]?[0-9]*|\s):([-]?[0-9]*|\s):([-]?[0-9]*|\s)
       
preg_match("/([-]?[0-9]*|\s?):([-]?[0-9]*|\s?):?([-]?[0-9]*|\s?)/", $pattern, $yakala);
       
$start = $yakala[1];
       
$stop = $yakala[2];
       
$step = $yakala[3];
       
        if(empty(
$start) && empty($stop) && $step == "-1"){//istisna durum
           
return strrev($str);
        }else if(empty(
$start) && empty($stop) && isset($step)){//istisna durum
           
$rev = "";
           
$yeni = "";
            if(
$step[0] == "-" && $stop != "-1"){$rev = "VAR";}
           
$atla = abs($step);
            for(
$i = 0; $i <= strlen($str); $i++){
               
$offset = $i*$atla;
                if(isset(
$str[$offset])){
                   
$yeni = $yeni.$str[$offset];
                }
            }
            if(
$rev != "VAR"){
                return
substr($yeni,0,strlen($str)-1);
               
//"hepsi boş, step dolu o da +";
           
}else{
                return
strrev(substr($yeni,0,strlen($str)-1));
               
//"hepsi boş, step dolu o da -";
           
}
        }
       
        if(empty(
$start) && empty($stop) && empty($step)){
            return
$str;
           
//"hepsi boş";
       
}else if(empty($start)){
            if(isset(
$stop) && empty($step)){
               
$rev = "";
                if(
$stop[0] == "-"){$rev = "VAR";}
                if(
$rev != "VAR"){
                    return
substr($str,0,$stop);
                   
//"start ve step boş, stop dolu"
               
}else{
                    return
strrev(substr($str,0,$stop));
                   
//"start ve step boş, stop -1";
               
}
            }else if(isset(
$stop) && isset($step)){
               
$rev = "";
                if(
$stop[0] == "-"){$rev = "VAR";}
               
$yeni = "";
                if(
$step == 1){
                    if(
$rev != "VAR"){
                        return
$str;
                       
//"start boş, stop ve step dolu, step 1";
                   
}else{
                        return
strrev(substr($str,0,abs($stop))); //abs -> mutlak değer (-5 = 5)
                        //"start boş, stop -, step dolu, step 1";
                   
}
                }else{
                   
$atla = abs($step);
                    for(
$i = 0; $i <= strlen($str); $i++){
                       
$offset = $i*$atla;
                        if(isset(
$str[$offset])){
                           
$yeni = $yeni.$str[$offset];
                        }
                    }
                    if(
$rev != "VAR"){
                        return
substr($yeni,0,$stop);
                       
//"start boş, step ve stop dolu";
                   
}else{
                        return
strrev(substr($yeni,0,abs($stop)));
                       
//"start boş, step ve stop -";
                   
}
                }
            }
       
//start boş değilse
       
}else if(!empty($start)){
            if(isset(
$stop) && empty($step)){
               
$rev = "";
                if(
$stop[0] == "-"){$rev = "VAR";}
                if(
$rev != "VAR"){
                    return
substr($str,$start,$stop);
                   
//return "step boş, start ve stop dolu";
               
}else{
                    return
strrev(substr($str,0,abs($stop)));
                   
//"step boş, start ve stop dolu, stop -";
               
}
            }else if(isset(
$stop) && isset($step)){
               
               
//hepsi dolu
               
$rev = "";
                if(
$stop[0] == "-"){$rev = "VAR";}
               
$yeni = "";
                if(
$step == 1){
                    if(
$rev != "VAR"){
                        return
substr($str,$start,$stop);
                       
//"hepsi dolu, step 1";
                   
}else{
                        return
substr($str,$start,abs($stop));
                       
//"hepsi dolu, step 1, stop -";
                   
}
                }else{
                    if(
$stop[0] == "-"){$rev = "VAR";}
                   
$atla = abs($step);
                    for(
$i = 0; $i <= strlen($str); $i++){
                       
$offset = $i*$atla;
                        if(isset(
$str[$offset])){
                           
$yeni = $yeni.$str[$offset];
                        }
                    }
                    if(
$rev != "VAR"){
                        return
substr($yeni,$start,$stop);
                       
//"hepsi dolu";
                   
}else{
                        return
strrev(substr($yeni,$start,abs($stop)));
                       
//"hepsi dolu, stop -";
                   
}
                }
            }
        }
    }
?>

Good works..
up
1
morgangalpin att gmail dotty com
10 years ago
Adding the $limit parameter introduced a bug that was not present in the original. If $limit is small or negative, a string with a length exceeding the limit can be returned. The $limit parameter should be checked. It takes slightly more processing, but it is dwarfed in comparison to the use of strlen().

<?php
 
function short_name($str, $limit)
  {
   
// Make sure a small or negative limit doesn't cause a negative length for substr().
   
if ($limit < 3)
    {
     
$limit = 3;
    }

   
// Now truncate the string if it is over the limit.
   
if (strlen($str) > $limit)
    {
      return
substr($str, 0, $limit - 3) . '...';
    }
    else
    {
      return
$str;
    }
  }
?>
up
0
Anonymous
2 days ago
Be aware of a slight inconsistency between substr and mb_substr

mb_substr("", 4);      returns empty string

substr("", 4);              returns boolean false

tested in PHP 7.1.11 (Fedora 26) and PHP 5.4.16 (CentOS 7.4)
up
0
kleeh at bellsouth dot net
6 months ago
If I may, I'd like to add an example for a bit more clarification on the action of substr(). There's a note saying "If string is equal to start characters long, an empty string will be returned. Prior to this version, FALSE was returned in this case. ".
That description puzzled me until I worked it out. So, if your string is "x" and you do 'substr("x",0, 1)' then you'll get "x" as expected. Same thing for substr("x",0,4)' or any other LENGTH parm. But if the START parm is equal to the LENGTH of the string, then you'll get an empty string (PHP 7) or FALSE (prior to PHP 7). An example of this would be 'substr("x", 1, 1)' , which would return either an empty string or FALSE. Because your "x", which has a LENGTH of '1', is in the 0th position, so 'starting' your substr request at position 1 results in the empty string (or FALSE) because there is nothing after your "x" in this example. Just trying to mitigate some head-scratching over the phrasing of the previous note.
up
0
m.m.j.kronenburg
1 year ago
<?php

/**
  * Returns and extracts the portion of string specified by the
  * start and length parameters from the original string.
  *
  * This function is simulair to function substr() except that it
  * removes the substring from the orignal string
  * (passed by reference).
  *
  * @param string $string      The input string.
  * @param integer $start      The start position (see substr() for
  *                            explanation).
  * @param integer $length     The length (see substr()
  *                            for explanation).
  * @return mixed              The substring or FALSE (see substr()
  *                            for explanation).
  **/
function substrex(&$string, $start, $length = PHP_INT_MAX)
{
  if(
$start > strlen($string)) { return false; }
  if(empty(
$length))           { return ''; }
  if(
$start < 0) { $start = max(0, $start + strlen($string)); }
 
$end = ($length < 0) ?
   
strlen($string) + $length :
   
min(strlen($string), $start + $length);
  if(
$end < $start) { return false; }
 
$length = $end - $start;
 
$substr = substr($string, $start, $length);
 
$string = substr($string, 0, $start).substr($string, $end); 
  return
$substr;
}

?>
up
0
Anonymous
3 years ago
substr() exhibits rather unexpected behavior with respect to when it returns "" and when it returns FALSE.  Specifically, the following appears to hold (tested on PHP 5.4.19):

*  if $string is "", it always returns FALSE

*  if ($start >= 0) and ($start >= strlen($string)) it returns FALSE

*  if ($start >= 0) and ($length<($start - strlen($string))) it returns FALSE

*  if ($start < 0) then it returns FALSE if *and only if* $length<-strlen($string).

No other cases return FALSE.
Note that in the last case, the position indicated by $start doesn't actually matter!

Also worth noting is that if you supply a $start that is < -strlen($string), it will be treated exactly as if you had set $start = strlen($string).

Here are some test cases to illustrate this behavior:

substr("",0,0) === FALSE
substr("",0,4) === FALSE
substr("",-2,4) === FALSE

substr("abc",3,0) === FALSE
substr("abc",3,10) === FALSE
substr("abc",3,-1) === FALSE
substr("abc",0,-4) === FALSE
substr("abc",2,-2) === FALSE
substr("abc",-1,-4) === FALSE
substr("abc",-4,-4) === FALSE
substr("abc",-5,-4) === FALSE
substr("abc",-10,-5) === FALSE

substr("abc",-10,-2) === "a"
substr("abc",-1,-3) === ""
substr("abc",-1,-2) === ""
substr("abc",-2,-3) === ""
substr("abc",0,0) === abc",0,0) === gv    &nbs === ""
substr("abc",0,0) === abc",0,0) === gv   i ===00,0) .."> 0
uass="key,0) === 7e> Hbr /itlpya^'br />substr("abc",-1,-4) === FALSE
substr_s="key,0) === 7e> Hbr /itlpya^'br />substr("abc",-1biv id="Vd115627"> on, /div> -voteun.m.jasons, ALSE wasat aluass="voteUniALauate" tis ($start >119852" title="_uniALau />*&,.4.,.4l$o[1]">);
  < likearsitle="2array_slice + jos="mechoPHPuggunctiobevothp?iotesLau/> ong>
on, If $limivalubsp5627"> - on, keyworbrarybstr("x",0, 1)' then you'll get "x" as expected. Same thing for s0901198>substr("abc",-1,-4) === FALSE
su0901198 />substr("abc",-1,-2) === ""
subst09011 === FALSE
substr("abc",-10,-5) === FALSE

substr("abc",-10,-2) === "a"
substr("abc",-1,-3) ==0901198 />substr("abc",-1,-2) === ""
subst09011 === FALSE
substr("abc",-10,-50,0) === abc",0,0) === gv    &nbs === ""
substr("abc",0,0) === abc",0,0) === gv&nb090119bspi ===00,0) .."> 0
uass="key,0) === 7e>090119b/itlpya^'br />substr("abc",-1,-4) === FALpheALSyubstr_s="key,0) === 7e> Hbr /itlpya^'br />substr("abc",-1biv i0901198>627"> ss="usernoall or negati 5627"> 5627">As per/>That ds v> $va="keyword">), $start + $length$string, $start, <lassblem. Hphivn t v> $va="keyword">), $start + $length$string, $ss="keyword">- $start, <$str<0384substr("abc",-1,-2) === ""
subst0384< === FALSE
substr("abc",-10,-5) === FALSE

substr("abc",-10,-2) === "a"
substr("abc",-1,-3) ==0384substr("abc",-1,-2) === ""
subst0384< === FALSE
substr("abc",-10,-50,0) === abc",0,0) === gv    &nbs === ""
substr("abc",0,0) === abc",0,0) === gv&nb0384 0 uass="key,0) === 7e>0384substr("abc",-1,-4) === FALQuicker== 7e> Hbr /itlpya^'br />substr("abc",-1biv i0384
5627">a href="#78019" class="name"> t), , + $length$start$length$substr , $start$i $string$stop$substr $length $string$stop$substr ">$stop), $length$substr , substr($string$stop$substr $stop$start$length$substr , $start$length$substr , $string$stop$substr $length$substr $ "namw FALed o) .."usern27"n,tion 0 , eg.:*/ classnan class="default">$substr $length$substr , <-he fospannuserout /div> v> <-he f ;su
&n,0, 1)' then you'll get "x" as expected. Same thing for 93884/span>$str93884/s />substr("abc",-1,-2) === ""
subs93884 === FALSE
substr("abc",-10,-5) === FALSE

substr("abc",-10,-2) === "a"
substr("abc",-1,-3) =93884/s />substr("abc",-1,-2) === ""
subs93884 === FALSE
substr("abc",-10,-50,0) === abc",0,0) === gv    &nbs === ""
substr("abc",0,0) === abc",0,0) === gv&n93884/bspi ===00,0) .."> 0 uass="key,0) === 7e93884/b/itlpya^'br />substr("abc",-1,-4) === FALSajkey,0str(mubstka class f Hbr /itlpya^'br />substr("abc",-1biv 93884/spiv>
, i-byl/votfe
; }
 
if ($length$string$i , was re>upsu< ire re> extndersitlbe m>, i-byl/votfe > down" tsolvsubstruing it
$
ng> * g> * "genanchor" href="#11 S="#11 itl"srd0ch"19852g> * "genanc ex $sndex Index s "x" aleted w."uan>)
hat it
hap; The start position (see substr() for
  *      tionApan class="default">$substr ($string, $start$length $startstr/div>  if(empty($length))    &nbs"){, un class="default">$substr ($string, $start, [strlen){-an>, [str />    {
      return
substrexpected. Same thing for 924($str924(substr("abc",-1,-2) === ""
subs924(< === FALSE
substr("abc",-10,-5) === FALSE

substr("abc",-10,-2) === "a"
substr("abc",-1,-3) =924(substr("abc",-1,-2) === ""
subs924(< === FALSE
substr("abc",-10,-50,0) === abc",0,0) === gv    &nbs === ""
substr("abc",0,0) === abc",0,0) === gv&n924( 0 uass="key,0) === 7e924(substr("abc",-1,-4) === FALgkhee.phorvotbstr_s="key,0) === 7e> Hbr /itlpya^'br />substr("abc",-1biv 924(
doon ofseen. 5627"> 5627">a href="#78019" class="name"> t; }
 
finbn o/a> yo.=30tion.substiot> fayl.exesp;         t   1198& &nb
               finbpan>[// Make sure a small or negative limit doesn't cause a negative length for substr().[if ($start$length), $start + $length[$stop$length),
$start $length), $start + $length[$stop$start, $length), $start + $length[, $length}
+
$length[$stop$start, , $substr $substr $substr , [ $
finbn 0iot> fayl.exe v> , substrexpected. Same thing for 91351/span>$str91351/s />substr("abc",-1,-2) === ""
subs91351 === FALSE
substr("abc",-10,-5) === FALSE

substr("abc",-10,-2) === "a"
substr("abc",-1,-3) =91351/s />substr("abc",-1,-2) === ""
subs91351 === FALSE
substr("abc",-10,-50,0) === abc",0,0) === gv    &nbs === ""
substr("abc",0,0) === abc",0,0) === gv&n91351/bspi ===00,0) .."> 0 uass="key,0) === 7e91351/b/itlpya^'br />substr("abc",-1,-4) === FALSaysanote y_s="k"52% l== 7e> Hbr /itlpya^'br />substr("abc",-1biv 91351/spiv>
dos&vfinb>(ev n"9852";vfinb>lov> >* 
; }
 
$length+
$length ¶eyword">-
$start// Make sure a small or nt; }
$length
¶)) -s="comment">// Make sure a small or nt; }
$length
+
$length ¶e 
if ( , 0 Anonymoussubstr("abc",-1,-2) === ""
subs9014 === FALSE
substr("abc",-10,-5) === FALSE

substr("abc",-10,-2) === "a"
substr("abc",-1,-3) =9014 />substr("abc",-1,-2) === ""
subs9014 === FALSE
substr("abc",-10,-50,0) === abc",0,0) === gv    &nbs === ""
substr("abc",0,0) === abc",0,0) === gv&n9014 bspi ===00,0) .."> 0 uass="key,0) === 7e9014 b/itlpya^'br />substr("abc",-1,-4) === FALsajjaotbstsajjaotey,0biz== 7e> Hbr /itlpya^'br />substr("abc",-1biv 9014
 utf-8ate" tisbehaviovery"talple!
  *      str("auun class="default">$substr
([$stop[$stop,
if (  &n class="d$9852t; }
if (if (  &n class="d$r nt; }
if (if (;
 
r==yword">),
);
 n>, substrexpected. Same thing for 8976r("x",0,4)' or any other LENGTH parm. Bu8976r(" />substr("abc",-1,-2) === ""
subs8976r === FALSE
substr("abc",-10,-5) === FALSE

substr("abc",-10,-2) === "a"
substr("abc",-1,-3) =8976r(" />substr("abc",-1,-2) === ""
subs8976r === FALSE
substr("abc",-10,-50,0) === abc",0,0) === gv    &nbs === ""
substr("abc",0,0) === abc",0,0) === gv&n8976r(bspi ===00,0) .."> 0
uass="key,0) === 7e8976r(b/itlpya^'br />substr("abc",-1,-4) === FALlink== 7e> Hbr /itlpya^'br />substr("abc",-1biv 8976r("xiv> uass="key,0) === 7e8024anb. Same t'br />substr("abc",-1,-4) === FALhighart<.."bstr_s="key,0) === 7e> Hbr /itlpya^'br />substr("abc",-1biv 8024an civ>
up strie"9852";v h-2 (equacass=rmw FAivn )nv>lected *:
<-hT" ass="userno$valubrn barraybeitlnd was clasx" abeginnng is "x" a h-2, phpcf19852" t) ..""I havesatcon 0user "x" an!" class s="phpcmiddlet alwwALSE wtt) .."" 0 ested ouan>eitlhavesed r ") .."" 0 ed ouihs havesone."v> <-hT" aass="keyss="usernos="telfeger $staorybs <eitlcue>i2, jripton, keywf19852" t) .."so: cue h-2(ically,, 1 ie>) ..""127hh43h2h 0 a
$substr ),   &n class="d$r nart) @returkeyword">){$length ), ), ) span class="default">$i $string  &n class="d$m out_to==yword">), }
),
$length}
),
  &n class="d$m out_tobsp;       }
),
$stop$startstr>);
$start > un class="default">$substr ($string, ), );
; }
 
))    &nb an>strlen trr negative limit doesn't cause a negative length for substr().$s==yword">), );strn class="defaun be returned. The $limit paramet trr negative limit doesn't cause a negative length for substr().< class="keyword">))    &nb ass="keyword">if () @retun class="defau"){; }
; }
  if(
$start strlen trr negative limit doesn't cause a negative length for substr().), ){){
}
un class="default">$substr
($string, $startif ()">);
){
}
$substr
($startif (art);
){$length }
) + )=s="default">$length
}
$start
$substr );
){$length}
un class="default">$substr
($startif (ord">, $start
if () + )pan>$substr );
strn class="defaulen; }
; }
  if(< trr negative limit doesn't cause a negative length for substr().< class="keyword">))    &nb an"keyword">; }
  if(
$start strlen trr negative limit doesn't cause a negative length for substr().), ){){
}
un class="default">$substr
($start
; }
  if(< trr negative limit doesn't cause a negative length for substr().< class="keyword">))    &nb an"keyword">; }
  if(
$start strlen trr negative limit doesn't cause a negative length for substr().), );
){
$start
> $substr ($startif (art);
){$length$substr un class="default">$substr ($startif (ord">, $sbsp;       );
){$length $start }
) + )=s="default">$length
$substr $start);
){$length$substr un class="default">$substr ($startif (ord">, $sbsp;       );
strn class="defaulen){){){
}
un class="default">$substr
($start
; }
strlenrouotun class="default">$substr ; }
  if(
$start strlenate the string if it is over t)))sskeyword">if ()">);
){
$start
> $substr ($startif (art}
$substr
($startif (art);
){$length$substr un class="default">$substr ($startif (ord">, $sbsp;       if () + )pan>$substr );
){$length $start }
) ?
   c",-;c",- returned. The $limit parametepos_panbsp;       }
) + )=s="default">$length
$substr $start$substr );
){$length$substr un class="default">$substr ($startif (ord">, $sbsp;       if () + )pan>$substr );
str>);
$start > un class="default">$substr ($string, ), );
; }
 
))    &nb an>strlen trr negative limit doesn't cause a negative length for substr().$s==yword">), );strn class="defau>);
, ))    &n">);
 > n>, substrexpected. Same thing for 53199n class="keyword">(subs53199 === FALSE
substr("abc",-10,-5) === FALSE

substr("abc",-10,-2) === "a"
substr("abc",-1,-3) =53199n />substr("abc",-1,-2) === ""
subs53199 === FALSE
substr("abc",-10,-50,0) === abc",0,0) === gv    &nbs === ""
substr("abc",0,0) === abc",0,0) === gv&n53199n=== abc"100,0) .."> 0 uass="key,0) === 7e53199n=. Same t'br />substr("abc",-1,-4) === FALbleakwi at msnkey,0) === 7e> Hbr /itlpya^'br />substr("abc",-1biv 53199n civ>
$stiotess="keyss="userns 0 , i-byl/votfeen.snumbertof tion.subst.0) .."mdiv>&cut() 0 bite_s=n(s=n>$s,c ex ss="key[,byl/vof >*&nbs])">);ie 0 http://www.weaverdream.l /div> $substr ($string, $sbsp;       if (if (; }
strlenate the string if it is over t) @return mixed d , $substr );
, $start);
), $start ))    &nb;>);
$length $start>">$stop$start$startalt&, $start$start++)u"){$length $substr $length [, $s==yword">), ){){
$substr $substr );
){);
$length un class="default">$substr ($start
$startsan>, r returned. The $limit paramet128bsp;       $length ; }
, $s==yword">), ){){
$substr );
$length un class="default">$substr ($start
$startsan>, if ($length ; }
, $s==yword">), ){){
; }
}
$substr $length
$substr );
){$start+keyword">if (; }
, [$length un class="default">$substr ($start$startsan>, r returned. The $limit paramet128bsp;       $length ; }
r returned. The $limit parametat">$s==yword">),
){){
$substr un class="default">$substr ($start
$startsan>, );
){$substr );
$length un class="default">$substr ($start
$startsan>, if ($length ; }
r returned. The $limit parametat">$s==yword">),
){){
$substr un class="default">$substr ($start
$startsan>, ; }
);
){; }
}
$substr $length
$substr );
){; }
}
$start+keyword">if (; }
, [);strn class="defauyword">, [ > n>, $substr );$length $start>">$stop$start$length $startalt&, $start$length $start++p"$length$substr $substr ($start$startsan>, $start){str /> n>, substrexpected. Same thing for 54281/span>$str54281/s />substr("abc",-1,-2) === ""
subs54281 === FALSE
substr("abc",-10,-5) === FALSE

substr("abc",-10,-2) === "a"
substr("abc",-1,-3) =54281/s />substr("abc",-1,-2) === ""
subs54281 === FALSE
substr("abc",-10,-50,0) === abc",0,0) === gv    &nbs === ""
substr("abc",0,0) === abc",0,0) === gv&n54281/bspi ===00,0) .."> 0
uass="key,0) === 7e54281/b. Same t'br />substr("abc",-1,-4) === FAL />_net at thomaskey,0trellakey,0de== 7e> Hbr /itlpya^'br />substr("abc",-1biv 54281/spiv>
a n!" claifubsax tionsbat a">){, ($startif (if (if (;
 
r==yword">),
){
),
$start $startif ();
str>);
}
$start
);
$start > $start);
$length $start>">$stop$start}
; }
, $start$start++) @retun class="defau"){r returned. The $limit parametar negative limit doesn't cause a) @retun class="defaun class="defaubreak;>);){
),
$start un class="default">$substr (if (;
 
>$sbsp;       if (
$start-an>, $s==yword">), if (;
 
r_tmp==yword">),
){
$start++;>);
){$length$start
> $startan>);strn class="defau returned. The $limit parameterVal"keyword">), $start un class="default">$substr (if (;
  $start
;
 
>$sbsp;       if (>$sbsp;       strn class="defau returned. The $limit parameterVal"keyword">), $substr );
, [ n>, substrexpected. Same thing for 81866n class="keyword">(subs81866 === FALSE
substr("abc",-10,-5) === FALSE

substr("abc",-10,-2) === "a"
substr("abc",-1,-3) =81866n />substr("abc",-1,-2) === ""
subs81866 === FALSE
substr("abc",-10,-50,0) === abc",0,0) === gv    &nbs === ""
substr("abc",0,0) === abc",0,0) === gv&n81866n=== abc"45,0) .."> 0
uass="key,0) === 7e81866n=. Same t'br />substr("abc",-1,-4) === FALAnonymous== 7e> Hbr /itlpya^'br />substr("abc",-1biv 81866n civ>
a n!" clato r&aarraybof n!" cls specifisubstrr&aarraybof ss="ke*:

, $string, );
) @retun class="defau"){[] an>}
un class="default">$substr
($string, $start;
  )">);
), $start un class="default">$substr ($string, )">);
strn class="defauyword">span>()an> n>,  n> ownon,r inpustwithoutu alw errorlhandl cl!keywordan>substrexpected. Same thing for 112745n class="keyword">(subs112745 === FALSE
substr("abc",-10,-5) === FALSE

substr("abc",-10,-2) === "a"
substr("abc",-1,-3) =112745n />substr("abc",-1,-2) === ""
subs112745 === FALSE
substr("abc",-10,-50,0) === abc",0,0) === gv    &nbs === ""
substr("abc",0,0) === abc",0,0) === gv&n112745n=== abc"40,0) .."> 0 uass="key,0) === 7e112745n=. Same t'br />substr("abc",-1,-4) === FALfs=".ly,bo"bstr_s="key,0) === 7e> Hbr /itlpya^'br />substr("abc",-1biv 112745n civ>

, b d d ,> <$a;, if (){strpan>( ;, if (){strpan>((; }
()akeyword">){strforault">$length $start>">$stop$start$length $startalt&>">$stop$start$length $start++)b d d ,> un class="default">$substr ( an class="default">$string$stop$string$stop$string){str,tio=s="default">$length(; }
(), , $start$substr ){){){){){){){){){){){){){){){){){){){){){){){){){){){){){){){){){){){){){){){){){){){){){){){){){){){){strpan>((; }
()akeyword">){strforault">$length $start>">$stop$start$length $startalt&>">$stop$start$length $start++)b d d ,> an class="default">$string$i$string){str,tio=s="default">$length(; }
(), , $start n>,  n>substrexpected. Same thing for 73233n class="keyword">(subs73233 === FALSE
substr("abc",-10,-5) === FALSE

substr("abc",-10,-2) === "a"
substr("abc",-1,-3) =73233n />substr("abc",-1,-2) === ""
subs73233 === FALSE
substr("abc",-10,-50,0) === abc",0,0) === gv    &nbs === ""
substr("abc",0,0) === abc",0,0) === gv&n73233n=== abc"40,0) .."> 0
uass="key,0) === 7e73233n=. Same t'br />substr("abc",-1,-4) === FALijavier aka(p; ims="ch) igjav== 7e> Hbr /itlpya^'br />substr("abc",-1biv 73233n civ>
, p> n> ;p; > < < $substr ($string$stop>">$stop$string$stop>">$stop$substr ($substr ($string(), $start $string$length ), if () { $length $start>">$stop[$string$i-an>, $start]!>">$stop$length $start++) { $length ; }
}
) { , un class="default">$substr ($string$stop$start}
) .upan>(; (un class="default">$substr ($string$stop$start}
-an>, $start+ult">$length [(; , $string$substr ($string$stop>">$stop$string$stop>">$stop$substr ($substr ($string$length $stringif () { $length $start>">$stop$start$length $string$i+keyword">if ($start]!>">$stop$length $start++) { if ($string$i+keyword">if ($start]) { , $string(un class="default">$substr ($string$stop$start}
+keyword">if ($start) .upan>(; , $stringsuytions, $string($string$stop>">$stop$string$stop>">$stop$substr ($substr ($string(), $start $string$length ), if () { $length $start>">$stop$length $string$i-an>, $start]!>">$stop$length $start++) { $length ; }
}
) { , un class="default">$substr ($string$stop$start}
) .upan>(; $length $string$i-an>, $start]=>">$stop; }
,
$string$i-an>, $start]=>">$stop; }
,
$string$i-an>, $start]=>">$stop$length $start++) {;} (un class="default">$substr ($string$stop$start}
-an>, $start+ult">$length [(; , $stringsubstrexpected. Same thing for 33737n class="keyword">(subs33737 === FALSE
substr("abc",-10,-5) === FALSE

substr("abc",-10,-2) === "a"
substr("abc",-1,-3) =33737n />substr("abc",-1,-2) === ""
subs33737 === FALSE
substr("abc",-10,-50,0) === abc",0,0) === gv    &nbs === ""
substr("abc",0,0) === abc",0,0) === gv&n33737n=== abc"40,0) .."> 0
uass="key,0) === 7e33737n=. Same t'br /bstr("abc",-1,-4) === FALAnonymous== 7e> Hbr /itlpya^'br />substr("abc",-1biv 33737n civ>
< p> n> < * ,> < * @ss="u n!" claspan> < * @ss="u ex $max (="defau 50) ,> < * @ss="u n!" clasreplace < * @yword">pan> < * @aut"ab david"bstephinknkey,0) = ,> < * @aut"ab loic"bstx=up" ey,0ne ,> < * @aut"ab arne ey,0harphprz"bstr_x ey,0net ,> < */ ($string, $start;
  if ((
),
$start ))    &nb; ,> < ), r returned. The $limit parametsmax="keyword">))    &nb , ))    &n; ,> < }
),
$start $start $string($start > $start(> $start), > $substr }
$start
($start > ), > $substr (> ($start > [), > $substr (> > (; }
>
), $start ))    &n; ,> $length ; }
, $string$i$start] =an>if ((),
$start un class="default">$substr (
$string
, $start;
  $start) .upan>($string(, $start $string((; }
>
), $start ($string$length ; }
, $string$i$start] =an>if ((),
$start un class="default">$substr (
$string
, $start;
  $start); (, (
$string(, , un class="default">$substr ($string, $start;
 
>$sbsp;       (, , un class="default">$substr ($string$start $length(if (;
  $start
$substr $substr $length(if (;
  if (;
&nartif ($substr $="defaultring sp; return substrexpected. Same thing for 86018n class="keyword">(subs86018 === FALSE
substr("abc",-10,-5) === FALSE

substr("abc",-10,-2) === "a"
substr("abc",-1,-3) =86018n />substr("abc",-1,-2) === ""
subs86018 === FALSE
substr("abc",-10,-50,0) === abc",0,0) === gv    &nbs === ""
substr("abc",0,0) === abc",0,0) === gv&n86018n=== abc"36,0) .."> 0
uass="key,0) === 7e86018n=. Same t'br />substr("abc",-1,-4) === FALmr.davin== 7e> Hbr /itlpya^'br />substr("abc",-1biv 86018n civ>
su: ,> < p> n>(if ((if ($length un class="default">$substr (if (([; }
$substr =ass="Pd">'s ,> $="defaultring sp; return substrexpected. Same thing for 73213n class="keyword">(subs73213 === FALSE
substr("abc",-10,-5) === FALSE

substr("abc",-10,-2) === "a"
substr("abc",-1,-3) =73213n />substr("abc",-1,-2) === ""
subs73213 === FALSE
substr("abc",-10,-50,0) === abc",0,0) === gv    &nbs === ""
substr("abc",0,0) === abc",0,0) === gv&n73213n=== abc"33,0) .."> 0
uass="key,0) === 7e73213n=. Same t'br />substr("abc",-1,-4) === FALper is-2)s"bstweb ey,0de== 7e> Hbr /itlpya^'br />substr("abc",-1biv 73213n civ>
< p> n>(> $substr (un class="default">$substr ($string, $start;
  $substr (, $start $substr
(un class="default">$substr ($string, $substr ;
  $substr ($start $substr (un class="default">$substr ($string, $string;
  $substr ($start un class="default">$substr
if (;
  > if (;
  > if (;
  $string>
$start$length ; }
, $start(; }
, $start(; }
>
(; }
>
$start), $start ,
(; }
>
$start), $start (; }
>
$start[$i$start] =d">> ; }
>
; }
,
$string> ; }
,
$start([$i$start> if (> ; }
,
if (> ; }
,
if ($length ; }
, $start$length [(; }
, $start(; }
>
$length $start, if ($length $start, $start$length $start, $start), $start , $length $start, if ($length $start, $start$length $start, $start), $start $length $start, if ($length $start, $start$length $start, $start[$i$start] =d">> ; }
>
; }
,
$string> ; }
,
$start(), $start $substr ($substr ())    &nspan>($substr $length $substr ">$stop$start$length $substr > $substr $length $substr <{ $length ))    &n>">$stop$start$length ))    &n; > ))    &n;ult">$length ))    &n++) ,> ($substr ($substr ;
  ))    &nss="keyword">;
  $substr (, $startif ($string(; }
, $startif ($string(), , $startif ($string(), , $substr }
$start un class="default">$substr
$substr ;
  ))    &n+, $string;
  ))    &n+,
$start;
  $string;
  $start
}
[$substr ;
  ))    &nss="keyword">;
  $substr ;
  $string>
; }
,
[$i$start]); (substrefault" hor"{own>ssectE
substr("abc",-redirectEhttp://frr />.net",-1,-2)en/
substr("ar /> add a n; &rn mall>/div>fault"rn e />su>v> < e />su>