How to extract substring by start-index and end-index?
Asked Answered
S

5

53
$str = 'HelloWorld';
$sub = substr($str, 3, 5);
echo $sub; // prints "loWor"

I know that substr() takes the first parameter, 2nd parameter is start index, while 3rd parameter is substring length to extract. What I need is to extract substring by startIndex and endIndex. What I need is something like this:

$str = 'HelloWorld';
$sub = my_substr_function($str, 3, 5);
echo $sub; // prints "lo"

Is there a function that does that in php? Or can you help me with a workaround solution, please?

Socio answered 11/8, 2011 at 21:35 Comment(2)
while the "workaround solution" is trivial, this is actually a good question, as most programming languages do have the two versions of the substring extraction function (usually horribly named "substr" and "substring") one with the length parameter and the other with the end-index parameter. It seems it's not the case of PHP.Orabelle
I wish PHP had this, like Javascript's substring. It's the little things like this that annoy me.Breezy
A
90

It's just math

$sub = substr($str, 3, 5 - 3);

The length is the end minus the start.

Alden answered 11/8, 2011 at 21:36 Comment(1)
i.e. There isn't a builtin PHP function to do so; you should use substr().Borreri
H
17
function my_substr_function($str, $start, $end)
{
  return substr($str, $start, $end - $start);
}

If you need to have it multibyte safe (i.e. for chinese characters, ...) use the mb_substr function:

function my_substr_function($str, $start, $end)
{
  return mb_substr($str, $start, $end - $start);
}
Historied answered 11/8, 2011 at 21:38 Comment(3)
I think It's more general and better answer than accepted one.Branscum
Shouldn't the inside substr(...) be mb_substr(...) - just in case..?Donaldson
Well, most of us probably don't deal with multibyte characters ... but yes, it does make sense and is valid comment (I extended my answer with your suggestion). Thanks, +1!Historied
F
10

Just subtract the start index from the end index and you have the length the function wants.

$start_index = 3;
$end_index = 5;
$sub = substr($str, $start_index, $end_index - $start_index);
Fibrinolysis answered 11/8, 2011 at 21:36 Comment(0)
I
5

You can just use a negative value on the third parameter:

echo substr('HelloWorld', 3, -5);
// will print "lo"

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).

As stated at the substr documentation.

Investment answered 6/2, 2017 at 18:47 Comment(0)
M
1

Not exactly...

If we have a start index as 0, and we want JUST the first char, it becomes difficult as this will not output what you want. So if your code is requiring an $end_index:

// We want just the first char only.
$start_index = 0;
$end_index = 0;
echo $str[$end_index - $start_index]; // One way... or...
if($end_index == 0) ++$end_index;
$sub = substr($str, $start_index, $end_index - $start_index);
echo $sub; // The other way.
Micron answered 22/1, 2014 at 6:42 Comment(3)
There @evilReiko, now you can see how substr() can be slightly more tricky.Micron
if I want only the first character, I'll use substr($str, 0, 1 - 0); I don't see anything tricky or complicatedSocio
The "tricky" part I was referring to was that if you have an end index that is 0 or less than the start index, you will have unexpected results. One way of ridding yourself of this problem is by the answer stated above. Your comment, 'if I want only the first...' tells me that you are in full control of the start and end index variables. If you give this control to a program, however, you could have a start variable and end variable such as $start_index = 0; $end_index = 0; or $start_index = 0; $end_index = -10;. You have to be careful on what controls these $variables. Thus the trickinessMicron

© 2022 - 2024 — McMap. All rights reserved.