I'm trying to figure out how to loop through a string similar to a directory structure, such as
/boom/stuff/test/file/check/
and I want to break off the last chunk on each look, then check that in a database until it finds something in the database, or runs out of string, so it should be:
/boom/stuff/test/file/check/ - 0 rows
/boom/stuff/test/file/ - 0 rows
/boom/stuff/test/ - 0 rows
/boom/stuff/ - 0 rows
/boom/ - 0 rows
/ - 0 rows
break; - no rows found at all.
or
/boom/stuff/test/file/check/ - 0 rows
/boom/stuff/test/file/ - 0 rows
/boom/stuff/test/ - 0 rows
/boom/stuff/ - 1 row
break; - found 1 row with /boom/stuff/
Does that make sense? Thanks.
Looping through string until string is empty
Moderator: General Moderators
-
blasterstudios
- Forum Newbie
- Posts: 13
- Joined: Thu Dec 23, 2004 10:08 am
- Location: Alabaster, AL
- Contact:
Re: Looping through string until string is empty
use explode to break the string from /
Then u can use count to count the number of items after breaking the string.
Then u can loop accordingly.
Then u can use count to count the number of items after breaking the string.
Then u can loop accordingly.
-
blasterstudios
- Forum Newbie
- Posts: 13
- Joined: Thu Dec 23, 2004 10:08 am
- Location: Alabaster, AL
- Contact:
Re: Looping through string until string is empty
I was really hoping to avoid a loop within a loop. My main while loop will check to see if there were any rows found in the database. If not, it will continue until one is found or the script will "break;" if it runs out of search. What I'm looking for really is something to take the original string and simply chop off the last fragment, leaving a trailing "/" (like my example).
I've attempted to use strrchr, but I think i'm using it wrong. it ends up in an infinite loop sometimes.
I've attempted to use strrchr, but I think i'm using it wrong. it ends up in an infinite loop sometimes.
Code: Select all
$check = "/my/crazy/long/url/";
$pageTotal = 0;
while($pageTotal <= 0){
if(strstr($check,"/")){
$check = strrchr($check,"/");
}
// check database and set $pageTotal variable
if($check == "/" || empty($check)){
break;
}
}-
blasterstudios
- Forum Newbie
- Posts: 13
- Joined: Thu Dec 23, 2004 10:08 am
- Location: Alabaster, AL
- Contact:
Re: Looping through string until string is empty
Ok, I just used this and so far it's working ok. Is there a better way?
Code: Select all
if(strstr($check,"/")){
$check = (substr($check,-1) == "/") ? substr($check,0,-1) : $check;
$check = substr($check,0,strrpos($check,"/")+1);
}Re: Looping through string until string is empty
for that u can do something like this.
$a=explode("/","/boom/stuff/test/file/check/");
$count=count($a);
Now you can get any value from a.
To get last value here u can do the following.
echo $a[$count-1];
$a=explode("/","/boom/stuff/test/file/check/");
$count=count($a);
Now you can get any value from a.
To get last value here u can do the following.
echo $a[$count-1];
-
blasterstudios
- Forum Newbie
- Posts: 13
- Joined: Thu Dec 23, 2004 10:08 am
- Location: Alabaster, AL
- Contact:
Re: Looping through string until string is empty
But the idea is that I have the string as a whole. I would have to recompile the string on each one to get the full url. I don't need parts, but need to chop off parts. I guess I could use what you're saying to do that, but it seems like more steps.