Forum Moderators: coopster
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! :)
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?
$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];
}