2

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

2 Answers 2

3

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:

function arabicSubStr ($value, $start, $length = -1) {
    if ($length == -1)
        $length = mb_strlen ($value, 'UTF-8') - $start;
    return mb_substr ($value, $start, $length, 'UTF-8');
}

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:

  • the mb_substr() function will only use it as a maximum value; and
  • you won't pass in any strings 10K or more.

In other words, something along the lines of:

function arabicSubStr ($value, $start, $length = 9999){
    return mb_substr ($value, $start, $length, 'UTF-8');
}

Though keep in mind I haven't tested that, I don't have any PHP environments at my current location.

Sign up to request clarification or add additional context in comments.

4 Comments

we are both right, The error is happen because i've defined $length as false,and i've came here to know how can i ignore the $length parametre and across to next one, and i though i've said i won't use strlen, in finally i think the only way is to use strlen, Thanks for your answer
@Alaa, give the 9999 option a go, it may work. I haven't tested it but since the doco states length is the maximum length to be extracted from the string, you might be pleasantly surprised.
i think strlen is much better, Thank you very much
i am really surprised but not because of your comment, because most people have the same misconception and that results a down vote for my question, i wish if i know how down voted me
0

It's because you have $length set to false as the default parameter for your function, which effectivley means you want it to return a substring of 0 length.

Unfortunately, if you have to set the final parameter (the charset) which I imagine you do, then you have to calculate the length of the string first, so something like:

function arabicSubStr($value,$start,$length=false){
    $length = ($length) ? $length : mb_strlen($value,'UTF-8') - $start;
    return mb_substr($value,$start,$length,'UTF-8');
}

3 Comments

The doco (php.net/manual/en/function.substr.php) seems to indicate otherwise. -1 strips off the last character.
-1 is not the default value for $length.
Oh crap. That's right. I hate that. I always expect it to work the other way around. -1 returns everything before the start point. Dumb. Will update.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.