Jump to content

create regex function help


emehrkay

Recommended Posts

I dont know much about regex, but i want to write a function that will allow the programmer to specify what they want to do with a string. So if they wanted it to be 1-5 chars in length they'd set the length attribute to '1-5'.

i want a set of basic attributes, that when set will add to the regex string in the right places. if not set, they'd return ''
i want to be able to say things like, only letters or numbers or doesnt start with... or whatever else anyone can think of

here is what i have, i dont know much so i just put the very basic part as xxx. so if none of the attributes are set, the value will be checked against xxx only. xxx being a basic regex that checks a string that other attributes could be dynamically added to - does that make sense?

you pass in an assoc. array
$arr = array = ('length' => '1-3', 'name' => 'test', 'value' => '1234');

[code]
<?php
public function strChk($arr){

$lenght = false;
$value = '';
$name = '';

foreach($arr as $key => $setVal){
$$key = $setVal;
}

$l = ($length) ? '['. $length .']' : '';

if(eregi($l.'xxx', $value))
return true;
}else{
return false;
}
}
?>
[/code]

so strChk($arr) should return false because it is more than three chars.
Link to comment
Share on other sites

It will be easier if you use the regex syntax for ranges--a comma instead of a hyphen. The user could specify:

# for an exact match
#, for a minimal match
,# for a maximum match
#,# for a ranged match

[code]
<?php
function strChk($arr){
extract($arr);
$l = ($length) ? '{' . $length . '}' : '' ;
return preg_match('/\A.' . $l . '\z/s', $value);
}

echo strChk(array('length' => '1,4', 'name' => 'test', 'value' => '1234')) ?
'Valid' : 'Invalid' ;
?>
[/code]
Link to comment
Share on other sites

thanks for your reply. I want to build the expression - get me.

so if i have

function check($arr){
$length = false;
$noNumStart = false;

foreach(){...//set the vals}

$l = ($length) ? '{' . $length . '}' : '' ;
$nn = ($noNumStart) ? 'dontKnowWhatToPutHere' : ''
//then i combine all of the attributes to build the expression so that it would work with or without the attribute being set to true. so if there is $a $b $c $d $e... attributes they can all be combined in the expression and evualated

preg_match($l.$nn.'xxx', $value) return true;
return false;
}

see what im saying? i know that i am asking a lot by not knowing regex. i think of it as buildign a dynamic query.
you start with "SELECT * FROM table" then you can easily add a bunch of stuff to it.

$query = "SELECT..."
$a = ($test) ? " WHERE this = that" : '';
$b ...
$query.$a.$b...
Link to comment
Share on other sites

If you're building an expression, what dictates the order in which you build it? You'd need to pass an array of associative arrays I'd imagine:

[code]function(array(array('letters' => 3), array('NaN' => 1)));[/code]

Resulting in "First, check for 3 letters; afterwards, check for a character that is not a number." Where the keys (letters, NaN) are "formats" that get their values (lengths) applied to them.

Am I getting closer?
Link to comment
Share on other sites

yes you are, i think you get what im trying to do.

Lets start at the end - lets say that we have an expression that will only allow for letters a-zA-Z or an underscore to be the first char, and it has to be between {5-10} chars in length <--those are the only requirements that i can think of for now.

i would assume it looked somehting like this (i dont know how to check for underscore in the beginning)

^[a-zA-Z/_]{5,10} //etc

or we can look at it like this

$allowedletters.$length.$baseExpression(which isnt in my lil example)

so for now, until i get a better understanding of it, i would say that the expression is built with a static order of variables. $a.$b.$c , but any one can be turned off ($b == '') or on ($b == '{5,10}' or whatever) and the expression still works

so i offer the function to a user and say; it can check your string against these boundaries (length, character etc) and they pass array = ('length' => '5-10', 'allowedChars' => 'a-zA-Z', 'value' => 'abcdefg') and it would return true just like if they were to pass array = ('allowedChars' => 'a-zA-Z', 'value' => 'abcdefg') //no length

does it make sense? is it possible? i guess i am trying to create an easy dynamic function instead of having N amount of static functions
Link to comment
Share on other sites

I follow, but the devil is in the details; Regular Expressions aren't the most easily configured toys.

Here's another example, using an order for more flexibility. It can be removed if you just need a set range of valid characters.

[code]
<pre>
<?php
function strChk($settings, $regex_pieces, $string){
$regex = '/';
if ($settings['anchor_BOL']) $regex .= '\A';
foreach ($regex_pieces as $piece) {
foreach ($piece as $type => $value) {
switch($type) {
case 'char':
$regex .= '[' . preg_quote($value) . ']';
break;
case 'length':
$regex .= '{' . $value . '}';
break;
}
}
}
if ($settings['anchor_EOL']) $regex .= '\z';
$regex .= '/';

echo "Running '$string' against $regex<br>Result: ";
return preg_match($regex, $string);
}

echo strChk(
array(
'anchor_BOL' => true,
'anchor_EOL' => true,
),
array(
array('char' => 'a-zA-Z'),
array('length' => '5,10'),
),
'abcde'
) ? 'Valid' : 'Invalid' ;
?>
</pre>
[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.