|
-
May 3rd, 2006, 07:24 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] PHP While....Loop - Help Needed
hi guyz, i have this question like;
In ASP there is something like
Code:
Do While arg
some more codes...
Loop
what that does in ASP/VB is that it loops until the argument is false
and i thought it should be doing the same thing with PHP's while function
but it doesn't instead it loops forever.
take this code for example;
Code:
'Note that this is an ASP Code
strPost = "[EDIT]C:\Program Files\bpmp\htdocs\smat_3.jpg[/EDIT]"
Do While InStr(1, strPost, "[EDITED]", 1) > 0 AND InStr(1, strPost, "[/EDITED]", 1) > 0
'some more codes...
Loop
but if i try that in PHP using the While....Loop then it loops for ever
pls note the function InStr() function,this is used to check if a given string occurs in another string and also it returns the position where the occurence starts and i have a similar php function that does that.
here it is.
PHP Code:
function InStr($string,$find,$CaseSensitive = false) {
$i=0;
while (strlen($string)>=$i) {
unset($substring);
if ($CaseSensitive) {
$find=strtolower($find);
$string=strtolower($string);
}
$substring=substr($string,$i,strlen($find));
if ($substring==$find) return $i;
$i++;
}
return -1;
}
Last edited by modpluz; May 3rd, 2006 at 09:42 PM.
-
May 3rd, 2006, 08:24 PM
#2
Re: PHP While....Loop - Help Needed
Use strpos() and stripos() to do an Instr.
-
May 3rd, 2006, 09:38 PM
#3
Thread Starter
Fanatic Member
Re: PHP While....Loop - Help Needed
it still does the same thing...it loops forever
-
May 3rd, 2006, 09:45 PM
#4
Re: PHP While....Loop - Help Needed
You don't need to write your own InStr function.
-
May 3rd, 2006, 09:52 PM
#5
Thread Starter
Fanatic Member
Re: PHP While....Loop - Help Needed
ok...am now using strpos() and it still does the same thing
-
May 3rd, 2006, 10:35 PM
#6
Re: PHP While....Loop - Help Needed
I am sorry that I can't post as long a post as I would like to explain.
strpos() and stripos() are builtin PHP functions that do what InStr does.
strpos() is case-sensitive and stripos() is not.
You would write this:
VB Code:
InStr(1, strPost, "[EDITED]", 1)
like this:
PHP Code:
strpos($strPost, '[EDITED]')
- it's the same function.
So there is no need to write your own function as they are already built in.
edit: Thanks for the merge Rob
Last edited by penagate; May 4th, 2006 at 07:35 AM.
-
May 8th, 2006, 01:06 PM
#7
Thread Starter
Fanatic Member
Re: PHP While....Loop - Help Needed
found a way to do this, instead of using the strpos() function, i did it this way.
i checked the string for all matches of [EDITED] by using the preg_match_all() function after which i used For...Loop to loop through the string and change whatever i wish to change.
-
May 8th, 2006, 02:07 PM
#8
Re: [RESOLVED] PHP While....Loop - Help Needed
Whats wrong with the strpos function? - it is a lot faster than pregh_match_all which actually, compiles, searches and stores internally the positon of every occurance of [EDITED] in the string.
-
May 9th, 2006, 07:27 AM
#9
Thread Starter
Fanatic Member
Re: [RESOLVED] PHP While....Loop - Help Needed
thanks...i don't know why it doesn't work for me.strpos() does get the position where the string [EDITED] started(i think).
but what if i have more than one [EDITED] in my string?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|