Help with a simple script


#1

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

For expample

[php]

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

#2

Have a look at stristr() http://us2.php.net/stristr


#3

Hello,

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.

[php]

<? $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"; } ?>

[/php]

Thanks,


#4

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.

http://us2.php.net/manual/en/ref.regex.php


#5

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

[php]

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

[/php]

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

I dont see why this doesnt work :(


#6

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.

[php]

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

[/php]