View previous topic :: View next topic |
Author |
Message |
TRUSTAbyss -
Joined: 29 Oct 2003 Posts: 3752 Location: USA, GA
|
Posted: Fri Oct 26, 2007 11:16 pm Post subject: Function: abyss_pattern_match() |
|
|
Hello Programmers!
I just wanted to let you know that I have another function for you PHP programmers. I've spent many hours trying to make this work and I finally managed to get it to work. The function is simple; it converts Abyss's patterns system into "Perl Compatible Regular Expressions." I would like to know what you guys think of it. I will be using this exact same function in FileLimit 3.0 when it's developed next year. Please let me know of any bugs you may find. Thank You! I hope Aprelium doesn't mind me posting this function because it mimic's their patterns system. ;-)
Here's the source code and a short example on using it. Use Abyss's Patterns documentation to test it. Works great! ;-)
Code: | <?php
/**
* AbyssWS Pattern Matching
* Function: abyss_pattern_match()
*
* Created by: Joshua H. (TRUSTAbyss)
*/
function abyss_pattern_match($pattern, $string)
{
// Create an array with the characters to be converted.
$replace = array('\\' => '\\\\', '/' => '\/', '\/' => '\/', '.' => '\.', '\.' => '\.', '+' => '\+', '\+' => '\+', '\*' => '\*', '*' => '.*', '\?' => '\?', '?' => '.', '$' => '\$', '\$' => '\$', '(' => '\(', '\(' => '\(', ')' => '\)', '\)' => '\)', '{' => '\{', '\{' => '\{', '}' => '\}', '\}' => '\}', '=' => '\=', '\=' => '\=', '!' => '\!', '\\!' => '\!', '<' => '\<', '\<' => '\<', '>' => '\>', '\>' => '\>', '|' => '\|', '\|' => '\|', ':' => '\:', '\:' => '\:', '\-' => '\-', '[!' => '[^', '^' => '\^', '\\^' => '\^', '[^' => '[^', '\[' => '\[', '\]' => '\]');
// Return TRUE or FALSE based on the response of the preg_match() function.
return preg_match("/^".strtr($pattern, $replace)."$/i", $string);
}
// Here's a short example of this function. It's just like using the eregi() function
// but instead you get to match Abyss Web Server's patterns.
// This pattern will match sub folders containing mp2, mp3, or mp4 files.
$pattern = "/*/*.mp[2-4]";
// Here's a sample string to be compared.
$string = "/folder/file.mp3";
if (abyss_pattern_match($pattern, $string)) // A match was found.
{
echo "Congratulations! This pattern was successfully found within the string";
}
else // No pattern match.
{
echo "Looks like the pattern didn't match.";
}
?> |
Note: This function is case-insensitive and returns a boolean value.
Sincerely, Josh. (TRUSTAbyss)
Last edited by TRUSTAbyss on Mon Aug 04, 2008 4:39 am; edited 4 times in total |
|
Back to top |
|
|
aprelium -
Joined: 22 Mar 2002 Posts: 6800
|
Posted: Tue Nov 13, 2007 3:13 pm Post subject: Re: Function: abyss_pattern_match() |
|
|
TRUSTAbyss,
Thank you for sharing your code snippet with the forum members. _________________ Support Team
Aprelium - http://www.aprelium.com |
|
Back to top |
|
|
TRUSTAbyss -
Joined: 29 Oct 2003 Posts: 3752 Location: USA, GA
|
Posted: Sun Aug 03, 2008 5:08 am Post subject: |
|
|
The code above has been updated to add a few improvements. The ^ and ! characters outside of a set needed to be escaped but they weren't properly escaped. This new updated function will automatically escape these two characters outside of a set or when they're in the wrong place in a set. Even though these characters are automatically escaped, you should always escape them anyway (read the manual for Abyss's Pattern Matching system). The function above has been tested as much as possible to prevent any accidental errors from bad syntax. This new improvement is important because Regular Expressions need to have certain characters escaped, so adding the auto-escape feature is a crucial step to having a more safer function.
Here's a list of the auto-escape characters (Regular Expression related):
Code: | . \ + ^ $ ( ) { } = ! < > | : |
Enjoy! |
|
Back to top |
|
|
JaturontThan -
Joined: 29 May 2008 Posts: 3
|
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|