i have problem with $length of substr function
my CODE
$string='I love stackoverflow.com';
function arabicSubStr($value,$start,$length=false){
return mb_substr($value,$start,$length,'UTF-8');
}
echo arabicSubStr($string,7);//outputs nothing
echo substr($string,7);//outputs stackoverflow.com
The reason of the problem is:
If length is given and is 0, FALSE or NULL an empty string will be returned.
So, How i can fix the problem?
i won’t use strlen($string)
EDITE
I know the reason is because i’ve defined $length as false
And i am here to know what should i put in $length parameter to avoid this error?
i am trying to put -1 it’s returns //stackoverflow.co
Since the reason you’re getting an empty string is specified entirely by the content of your question (using 0, FALSE or NULL), I assume you just want a way to get the rest of the string.
In which case, I’d use something like:
You need to do it this way since there is no sentinel value of length that means “the rest of the string”. Positive numbers (and zero) will limit the size to that given, negative numbers will strip off the end of the string (as you show in your question edit).
If you really don’t want to use a string length function, you could try a value of 9999 (or even higher) and hope that:
mb_substr()function will only use it as a maximum value; andIn other words, something along the lines of:
Though keep in mind I haven’t tested that, I don’t have any PHP environments at my current location.