Help with a simple script

I want to to make an if command which checks if a variable has htdocs in it.

For expample


<? $path = "c:apache2htdocs" // Similar to this but will allow anything with htdocs in. if ( $path == "htdocs" { Granted } [/php]

Have a look at stristr()


Thanks for the link, the code works great.

But I would like to expand on this. I would like it to be able to look up several words/letters.

Note: it does not have to be an array, I had it because it was the only thing I could think of.

The following code allways goes through denied part.


<? $variable = 'potatoes are great'; $have = array( 'z', 'q', 'f', 'a'); if(stristr($variable, $have ) === False) { //if it is NOT included it should go through here echo "Denied"; } else { //if it is included it should go through here echo "Allowed"; } ?>



I would use stristr if it were something relatively simple… Perhaps you might want to look at using regular expressions. They are a bit more difficult (as far as the syntax) but very powerful.

Using something like stristr() you would need to continually loop and test for each situation (or character as you have shown) where regular expressions would make it a one time look and be much quicker.

Thanks for the link, i am using the page you sent me for a different problem.


<? $path ='c:\Apache2\htdocs\images\'; $path = ereg_replace("c:A", "", $path); echo $path; ?>


I think this should give me pache2htdocsimages but it just returns c:Apache2htdocsimages

I dont see why this doesnt work :(

Because the backslash character is a “Special Character” it’s a bit tougher to deal with.

In your Original Path you have double backslashes because they need to be escaped to be displayed properly. However they don’t need to be escaped for the ereg_replace, but they DO need to be escaped for the echo.

So in the ereg_replace you are searching for c:A however, at this point it doesn’t find it because ereg_replace seeks [b]c:\Apache2\htdocs\images[/b] (yes it sees the “Escaped” backslashes as well.

In the ereg_replace when looking for a special character, you need to escape it. So in this case you need to escape the backslashes you are looking for. Thus you will need FOUR backslashes in the Search portion of ereg_replace.


<? $path ="c:\Apache2\htdocs\images"; $path = ereg_replace('c:\\A', " ", $path); echo $path; ?>


Sponsor our Newsletter | Privacy Policy | Terms of Service