| Replace deprecated function What to use as replacement? |
jeffshead

msg:4375232 | 9:38 pm on Oct 16, 2011 (gmt 0) | I know very little about PHP but I have a script (I did not write it) that I need to use with PHP 5.3x and it has several deprecated functions. I've googled and I have found examples that have helped me replace all of the functions except for the one below:
if(file_exists($file) && strrchr($fileName, '.') == '.flv' && strlen($fileName) > 2 && !eregi(basename($_SERVER['PHP_SELF']), $fileName) && ereg('^[^./][^/]*$', $fileName))
Can someone tell me if this is correct:
if(file_exists($file) && strrchr($fileName, '.') == '.flv' && strlen($fileName) > 2 && !preg_match("/" . basename($_SERVER['PHP_SELF']) . "/i", $fileName) && preg_match('|^[^./][^/]*$|', $fileName))
Since I'm a novice, I'm not sure what this particular code does so I don't know if this fixed it or not.
|
rocknbil

msg:4375537 | 6:19 pm on Oct 17, 2011 (gmt 0) | Did you test it, looks like you got it! How to test (the script won't run until all stuff is fixed, I know: ) You pull out a piece and put it in a test file, like <?php $fileName = 'testme/'; if (preg_match('|^[^./][^/]*$|', $fileName)) { echo "works"; } else { echo "Fails"; } ?> | I'm not sure what this particular code does |
| The basics of regular expression pattern matching is delimiter-pattern-delimiter-modifiers. Using the previous: ' = this is the PHP delimiter for the pattern - it has nothing to do with regular expressions, it's just how you use PHP's preg functions. Completely equivalent would be "|^[^./][^/]*$|" | = This is the opening delimiter for the regex. A delimiter can be ANYTHING, so long as it's not one of the characters you're matching on (or, you escape that character within the regex.) The "usual" delimiters are /, but they used the pipe | because / is part of the pattern, and they didn't want to escape it. All of these are exactly the same as your original. '/^[^.\/][^\/]*$/' (escaped) '-^[^./][^/]*$-' '_^[^./][^/]*$_' Because metacharacters (- ^ | $ !, etc.) are very important in regex's depending on the context (WHERE you use them), this can get very confusing very fast. :-) the dash -, for example, when used within a character class means a range. ([a-z] = letters a to z.) ^ = This is one of those cases of context. When the first character in a pattern, it means "the string I'm checking the pattern against starts with." Within a character class, it is a negation operator (below.) [] = The brackets create a class, that is, normally, "match on all of these characters." [^./] Since the first character in the class is the carat, in this context it means, "anything NOT these characters." Because it "stands alone" without a quantifier, is also means "one and ONLY one of these." So all together, [^./] means "one and only one of either a dot or a /" Some examples of quantifiers beyond "one and only one," [^./]+ = One or more of either a dot or a slash [^./]? = Zero or one of either a dot or a slash [^./]* = Zero or any number of of either a dot or a slash [^./]{1,3} = no less than one or no more than 3 of either a dot or a slash The dot character is another contextual "gotcha" - outside of a class, it means "zero or more of any character." Inside a character class like that, it's just a character, the dot. [^/]* = You probably got it from the previous - following the first part, it's followed by zero or more of anything NOT a /. $ = Like a bare ^ at the beginning, this means "The string I'm checking the pattern against ends here." | = End of the pattern Modifiers: After the pattern delimiter and before the ending PHP string delimiter you may have modifiers, which tell the engine how to manage the match - for example, "i" is "treat it as case-insensitive." You can see the modifier in your second one, !preg_match("/" . basename($_SERVER['PHP_SELF']) . "/i", $fileName) For example, if you're trying to match on the word "file," it will match either FILE or file or fIlE, regexes are not case-insensitive by default. ' = PHP Ending of the pattern match string. All together, '|^[^./][^/]*$|' means Match on a string that starts with one or more characters that are NOT a dot or a slash and ends with zero or more characters that are not a slash.
|
|
|