Frank Stanovcak wrote:
> A while ago I asked a question and got a few answers, so I thought I would
> toss this out there as a follow up. I'm going to be using this to return
> filtered regexp values for a user interface.
>
> I haven't had a chance to enter this into my code yet, so if anyone sees
> something wrong please hammer away, otherwise I hope it helps save some one
> some time.
>
To me, the name of your function indicates that you are making sure that the
regex is good. Not that you are checking input against a regex. I would use
something like match_to_regex() or something like that. My naming
conventions have never really been clear to anybody but me, so you might have
a better suggestion...
> function regexp_sanitize($string,$regexp) {
is, isarray() your own function? PHP is is_array()
> if(isarray($string)) {
> foreach($string as $key => $value) {
This line is broken... Should it not end with a semi-colon?
> $count = preg_match($regexp,$value,$matches) {
> if($count != 1) {
# You are over writing the previous out put, should that be $results[$key][] =
> $result[$key] = FALSE;
> } else {
> foreach($matches as $toss => $matchval) {
# You are over writing the previous out put, should that be $results[$key][] =
> $result[$key] = $matchval;
> };
> };
> };
> } else {
> $count = preg_match($regexp,$string,$matches);
> if($count != 1) {
> $result = FALSE;
> } else {
> $result = $matches[0];
> };
> };
> return($result);
> };
>
what is up with all the semi-colons? They are not needed.
>
>
Personally, I would do it this way.
<?php
function match_to_regexp($input, $regexp) {
if ( !is_array($input) ) {
$input = array($input);
}
foreach($input AS $key => $value) {
if( preg_match_all($regexp, $value, $matches, PREG_SET_ORDER) ) {
array_shift($matches);
$result[$key] = $matches;
} else {
$result[$key][] = FALSE;
}
}
return $result;
}
?>
Hope this helps.
--
Jim Lucas
"Some men are born to greatness, some achieve greatness,
and some have greatness thrust upon them."
Twelfth Night, Act II, Scene V
by William Shakespeare
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php