Forum Moderators: coopster

Message Too Old, No Replies

how to find the NEXT-TO-LAST occurence of substring in a string

         

benlieb

4:16 am on May 18, 2006 (gmt 0)

10+ Year Member



I can't think of any smart ways of getting the postition of the nth occurence of a substring within a string. Nor can I think of a way to find the nth-to-last occurence (like 2nd to last, or 3rd to last).

Any help?

$string = "text/somemoretext/evenmore/something/hithere/imhere";

I would like to to be able to access subections of the string above from any given "/" to the end, counting either fromt the front or the back. Please help!

I know you guys and gals are geniuses here! :)

Steerpike

4:40 am on May 18, 2006 (gmt 0)

10+ Year Member




$words = explode("/", $string);

$word = substr($words[1], 4, 7);

This returns 'more' from the 'somemoretext' section.
You can use sizeof($word[n]); to get how many characters a particular element of $words has.

Although I think I'vemisunerstood the question?

benlieb

5:07 am on May 18, 2006 (gmt 0)

10+ Year Member



Steerpike: thanks for the reply.

I think you did misunderstand the question.

$string = "text/somemoretext/evenmore/something/hithere/imhere";

There are 5 "/" in the above string. Lets assume there could be an infinite number (n) of them in reality.

I want to access a substring of string from number n "/" to the end.

For example the following substring would be the nth occurence of "/" where n = 3:

"/something/hithere/imhere"

n = 2:

"/evenmore/something/hithere/imhere"

And ideally, counting backwards would be available.

n = -2 (i.e., the 2nd to last occurence of "/"):

"/hithere/imhere"

Does this make any sense?

If this were a native function of php it would look like this:

$sub = substring($string, "/", $n),

where $n above can be any number, positive or negative, indicating the direction (counting forwards or backwards). This could already exist, but I just can't find it.

Does that make sense?

Steerpike

5:29 am on May 18, 2006 (gmt 0)

10+ Year Member



From what you describe, you'll still need to explode the original string into an array since
each part of the string can be of any length and the only way of knowing its beginning and
end is by the position of the /'s.
Once you have an array of each word you can perform any kind of manipulation you mentioned by
looping through the array starting from different elements or even from starting at the end
of the array.

$string = "text/somemoretext/evenmore/something/hithere/imhere";

$words = explode("/", $string);
$counter = sizeof($words);
$x=0;
for($x; $x<$counter; $x++)
{
echo $words[$x];
}
This will output:
textsomemoretextevenmoresomethinghithereimhere

Change value of $x to get different chunks:
ie,
$x = 3;
Outputs:
hithereimhere

For backwards:
$x = $counter;
for($x; $x=>0; $x--)
{
echo $words[x];
}

benlieb

5:55 am on May 18, 2006 (gmt 0)

10+ Year Member



Steerpike: Thanks again for the reply. I think by this method I could accomplish what I need. It seems like there should be an easier way, but I can't think of anything that wouldn't use explode, so that's what I'll do.

Thanks again.