Begin forwarded message:
>
> Revision
> 4217
> Author
> mikelietz
> Date
> 2010-05-16 03:41:26 +0000 (Sun, 16 May 2010)
> Log Message
>
> Add a utility function for finding usable regex delimiters, from #1225.
> Thanks RoUS!
> Modified Paths
>
> trunk/htdocs/system/classes/utils.php
> Diff
>
> Modified: trunk/htdocs/system/classes/utils.php (4216 => 4217)
>
> --- trunk/htdocs/system/classes/utils.php 2010-05-11 09:14:28 UTC (rev
> 4216)
> +++ trunk/htdocs/system/classes/utils.php 2010-05-16 03:41:26 UTC (rev
> 4217)
> @@ -1089,5 +1089,55 @@
> {
> return htmlspecialchars( $string, $quote_flag, $encoding );
> }
> +
> + /**
> + * Convenience function to find a usable PCRE regular expression
> + * delimiter for a particular string. (I.e., some character that
> + * *isn't* found in the string.)
> + *
> + * @param $string. string. The string for which to find a delimiter.
> + * @param $choices. string. Delimiters from which to choose one.
> + * @param $encoding. string. The encoding of the passed string
> + *
> + * @return A valid regex delimiter, or null if none of the choices work.
> + */
> + public static function regexdelim( $string, $choices=null )
> + {
> + /*
> + * Supply some default possibilities for delimiters if we
> + * weren't given an explicit list.
> + */
> + if ( ! isset( $choices ) )
> + {
> + $choices = sprintf('%c%c%c%c%c%c%c',
> + 167, /* § */
> + 164, /* ¤ */
> + 165, /* ¥ */
> + ord('`'),
> + ord('~'),
> + ord('%'),
> + ord('#')
> + );
> + }
> + $a_delims = str_split( $choices );
> + /*
> + * Default condition is 'we didn't find one.'
> + */
> + $delim = null;
> + /*
> + * Check for each possibility by scanning the text for it.
> + * If it isn't found, it's a valid choice, so break out of the
> + * loop.
> + */
> + foreach ( $a_delims as $tdelim )
> + {
> + if ( ! strstr( $string, $tdelim ) )
> + {
> + $delim = $tdelim;
> + break;
> + }
> + }
> + return $delim;
> + }
> }
> ?>
I'm assuming the problem the above function solves is we'll have strings such
as "foo/bar" to turn into regular expressions. So you'd end up with "§foo/bar§"
using the function above.
But what happens when you have a string that contains *all* the above
characters, and indeed any valid regex delimiting character in PHP? Then the
above function fails and you can't build a valid regex.
You can escape the string as you wrap the delimiters around it, which will work
no matter what the string contains - it can contain all the valid delimiters
and still work. PHP even has a built in function to escape characters in the
string for us - addcslashes!
addcslashes("foo/bar", "/"); # => "foo\/bar"
C
---
Caius Durling
[email protected]
+44 (0) 7960 268 100
http://caius.name/
--
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at http://groups.google.com/group/habari-dev