Author |
Message |
killing-hours
RavenNuke(tm) Development Team
![](modules/Forums/images/avatars/18f54b284d120ece8c103.gif)
Joined: Oct 01, 2010
Posts: 438
Location: Houston, Tx
|
Posted:
Wed Apr 20, 2011 1:27 pm |
|
Hey all-
Looking for a nudge.
I've got two variables that I want to compare and return a message back of either "Found" or "Not Found". I've read up on the "preg_match" & "strstr" but I'm failing to get the syntax correct or understand the way this function works. (everything i've read says to use strstr (or stristr) over preg_match as it's faster)
exampe: Find 2 inside of 1
Code:$var1, $var2:
if(stristr($var1,$var2)) {
echo 'Found';
}else{
echo 'Not Found'
}
|
|
_________________ Money is the measurement of time - Me
"You can all go to hell…I’m going to Texas" -Davy Crockett |
|
|
![](themes/RavenIce/forums/images/spacer.gif) |
Palbin
Site Admin
![](modules/Forums/images/avatars/Dilbert/Dilbert_-_Dogbert_King.gif)
Joined: Mar 30, 2006
Posts: 2583
Location: Pittsburgh, Pennsylvania
|
Posted:
Wed Apr 20, 2011 2:06 pm |
|
you should be using the str functions if it is a simple comparison or search. If it is not obviously you would need to use the preg functions.
Are you looking for comments on the code? What you posted should be fine, but it is hard to say with out knowing what the variables contain. Also make sure they are not integers or arrays. |
_________________ "Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan. |
|
|
![](themes/RavenIce/forums/images/spacer.gif) |
killing-hours
![](modules/Forums/images/avatars/gallery/blank.gif)
|
Posted:
Wed Apr 20, 2011 2:14 pm |
|
That is what I was checking on as we you posted Palbin.
This function will compare a posted file type against the allowed file types.
So it would be like this.
(I'm adding spaces between the single quotes just so it's easier to read here)
$var1 = ' jpg ';
$var2 = ' *.jpg;*.png;*.gif; ';
I also didn't post the aforementioned example correctly... it should read:
Code:if(stristr($var1,$var2) === TRUE) {
echo 'Found';
}else{
echo 'Not Found';
}
|
Since stristr returned the "leftovers" from the first match given the original post.
However, I still never returned "TRUE" even if I literally put the strings instead of the variables.
i.e.
Code:if(stristr('jpg','jpg') === TRUE) etc etc
|
I also thought that possibly the other characters may be throwing it off... so I tried str_replace and used an array to remove those other characters... still with no luck. Seems I'm doing it right... but it's just not playing nice with me. Must mean it's something simple I'm not seeing right in front of my face.
Here's the actual function in case you see something I'm missing
Code:if(stristr('jpg' , 'jpg') === TRUE){
echo 'Found';
}else{
echo 'Not Found';
}
|
-------------
GAHHHHHHH... nvm... seems I don't need the " === TRUE" part in there. Thanks PHP.net sheesh. |
|
|
|
![](themes/RavenIce/forums/images/spacer.gif) |
Palbin
![](modules/Forums/images/avatars/gallery/blank.gif)
|
Posted:
Wed Apr 20, 2011 3:43 pm |
|
Quote: |
A single = sign is an assignment - take what's on the right as an expression and save it in the variable named on the left.
A double = sign is a comparison and tests whether the variable / expression / constant to the left has the same value as the variable / expression / constant to the right.
A triple = sign is a comparison to see whether two variables / expressions / constants are equal AND have the same type - i.e. both are strings or both are integers.
|
stristr does not return a boolean value (true/false) it returns a string unless it it does not find anything, and then it will return false. If you want the TRUE in there then you only want two equal signs. |
|
|
|
![](themes/RavenIce/forums/images/spacer.gif) |
killing-hours
![](modules/Forums/images/avatars/gallery/blank.gif)
|
Posted:
Wed Apr 20, 2011 3:49 pm |
|
If that's the case... (and this is just my mind inquiring)... how is it that I can process the "if/then" statement without an " == TRUE" in there if it's only returning back a string and not a true statement?
I removed the triple FALSE and it works exactly how I want it to... but now you've got me curious if that's the way I should be using it.
I.e.
Code:if(stristr($var1,$var2)){
// do this
}else{
// throw an error
}
|
|
|
|
|
![](themes/RavenIce/forums/images/spacer.gif) |
Palbin
![](modules/Forums/images/avatars/gallery/blank.gif)
|
Posted:
Wed Apr 20, 2011 9:07 pm |
|
When you do an if it automatically does a true false comparison. The following are false:
FALSE
0
''(empty)
The following are true:
TRUE
1
ANY THING ELSE |
|
|
|
![](themes/RavenIce/forums/images/spacer.gif) |
killing-hours
![](modules/Forums/images/avatars/gallery/blank.gif)
|
Posted:
Thu Apr 21, 2011 6:53 am |
|
Thank you sir... That makes perfect sense. Learn something new everyday.
(Remember this ad campaign?) ![Wink](modules/Forums/images/smiles/icon_wink.gif) |
|
|
|
![](themes/RavenIce/forums/images/spacer.gif) |
Guardian2003
Site Admin
![](modules/Forums/images/avatars/125904890252d880f79f312.png)
Joined: Aug 28, 2003
Posts: 6799
Location: Ha Noi, Viet Nam
|
Posted:
Thu Apr 21, 2011 3:20 pm |
|
Hmm......
Just make SURE you are matching on the last three characters of the file name not anywhere within the filename so you cannot do e.g. myfilename.php.jpg.php
If this is part of an upload sequence, make sure you change the final stored filename to something different than what the original filename was.
Most hosting companies are wiser now and correctly handle different mimetypes (to prevent executing code masquerading as images) but just in case, a normal user uploading a file should never be able to guess what a direct link to the file might be. |
|
|
|
![](themes/RavenIce/forums/images/spacer.gif) |
killing-hours
![](modules/Forums/images/avatars/gallery/blank.gif)
|
Posted:
Thu Apr 21, 2011 3:23 pm |
|
Already have that covered. md5'ing it with a new file name Only retaining the original filename in the database for easy reference. |
|
|
|
![](themes/RavenIce/forums/images/spacer.gif) |
|