Function: abyss_pattern_match()

 
Post new topic   Reply to topic    Aprelium Forum Index -> PHP
View previous topic :: View next topic  
Author Message
TRUSTAbyss
-


Joined: 29 Oct 2003
Posts: 3752
Location: USA, GA

PostPosted: Fri Oct 26, 2007 11:16 pm    Post subject: Function: abyss_pattern_match() Reply with quote

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 View user's profile Send private message Visit poster's website
aprelium
-


Joined: 22 Mar 2002
Posts: 6800

PostPosted: Tue Nov 13, 2007 3:13 pm    Post subject: Re: Function: abyss_pattern_match() Reply with quote

TRUSTAbyss,

Thank you for sharing your code snippet with the forum members.
_________________
Support Team
Aprelium - http://www.aprelium.com
Back to top View user's profile Send private message Send e-mail
TRUSTAbyss
-


Joined: 29 Oct 2003
Posts: 3752
Location: USA, GA

PostPosted: Sun Aug 03, 2008 5:08 am    Post subject: Reply with quote

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 View user's profile Send private message Visit poster's website
JaturontThan
-


Joined: 29 May 2008
Posts: 3

PostPosted: Mon Aug 11, 2008 8:40 am    Post subject: Reply with quote

Thanks I'll try it out :)
_________________
Cosmetic Surgery Thailand
Plastic Surgery Thailand
Back to top View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Aprelium Forum Index -> PHP All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
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


Powered by phpBB phpBB Group