Jump to content

Include any character between 2 and 100


doubledee

Recommended Posts

Thanks to the advice of others on PHPFreaks, I have decided that allowing any character for certain fields on my "Add an Article" form.  However, I still want to define a length between 2 and 100.

 

How do I re-write my RegEx to accomplish this?

 

Before:

// Check HTML Title.
if (empty($trimmed['htmlTitle'])){
$errors['htmlTitle'] = 'Please enter an HTML Title.';
}else{
if (preg_match('#^[A-Z \'.-]{2,100}$#i', $trimmed['htmlTitle'])){
	$htmlTitle = $trimmed['htmlTitle'];
}else{
	$errors['htmlTitle'] = 'HTML Title must be 2-100 characters (A-Z \' . -)';
}
}

 

 

Here is my best guess...

 

After:

// Check HTML Title.
if (empty($trimmed['htmlTitle'])){
$errors['htmlTitle'] = 'Please enter an HTML Title.';
}else{
if (preg_match('#^[.*]{2,100}$#i', $trimmed['htmlTitle'])){
	$htmlTitle = $trimmed['htmlTitle'];
}else{
	$errors['htmlTitle'] = 'HTML Title must be 2-100 characters (A-Z \' . -)';
}
}

 

 

Debbie

 

 

Link to comment
Share on other sites

That is simply checking a string length, which is what strlen() is for.

if( strlen($variable) > 1 && strlen($variable) < 101 ) {

 

I can see how that would work, but let's say I want a more robust solution that says...

 

"Allow an combination of characters (on a U.S.) keyboard including whitespaces, but the string must not start with a whitespace or be entirely whitespaces and it should be between 2 and 100 characters."

 

That would allow someone to enter anything for the field "HTML Description" however it would prevent someone from entering all whitespaces.

 

That is why I usually lean towards Regular Expressions - they are better equipped to handle what I just described.

 

 

Debbie

 

P.S.  I looked at the link to StackOverflow, but it doesn't prevent someone entering in all whitespaces.

 

 

Link to comment
Share on other sites

You said any characters, with a length between 2 and 100 characters. You don't need a pattern for that even with the whitespace requirement. trim() will remove leading and trailing whitespace, thus if all whitespace is entered, the string will be empty and fail the strlen() tests.

Link to comment
Share on other sites

So use:

$variable = trim($variable);
$length = strlen($variable);
if($length > 1 && $length < 101) {
  //do something
}

 

You are trying to use a function, for no other reason than you WANT to use it.  Even though it would be using un-needed overhead.

Link to comment
Share on other sites

then add

/s matches any and all whitespace character

 

You didn't read what I said...

 

"Allow an combination of characters (on a U.S.) keyboard including whitespaces, but the string must not start with a whitespace or be entirely whitespaces and it should be between 2 and 100 characters."

 

 

Using /s would allow someone to enter 100 spaces - hardly a good "HTML Title"?! 

 

 

Debbie

 

 

Link to comment
Share on other sites

You said any characters, with a length between 2 and 100 characters. You don't need a pattern for that even with the whitespace requirement. trim() will remove leading and trailing whitespace, thus if all whitespace is entered, the string will be empty and fail the strlen() tests.

 

100 spaces would be a zero-length string w.r.t. strlen()??

 

 

Debbie

 

Link to comment
Share on other sites

So use:

$variable = trim($variable);
$length = strlen($variable);
if($length > 1 && $length < 101) {
  //do something
}

 

You are trying to use a function, for no other reason than you WANT to use it.  Even though it would be using un-needed overhead.

 

1.) Why is using a Regex "un-needed overhead"?

 

2.) No, I'm trying to learn how to do something multiple ways.

 

3.) I prefer Regex because I can do in one line what code usually takes several likes and often isn't as thorough.

 

So, now I know how to do what I want using PHP functions and code, but I'd still be curious to see how to do this using Regex, because it seems like a very common need?!

 

 

Debbie

 

 

Link to comment
Share on other sites

ah wrong case

 

/S matches any character that is not a white space

 

Okay, but then you'd be forced to have a contiguous string of characters from length 2 to 100.

 

With just \S you would be able to have a sentence, e.g.

 

"Postage Meters can save your business money!!"

 

 

Debbie

 

 

Link to comment
Share on other sites

^[ \t]+|[ \t]+$ matches leading and trailing whitespace (TABS or SPACES )

 

that is if you don't want to strip whitespace and have something like

 

thisisacontinuouslinewithoutspacesortabs

 

 

then you would have "This is a line without a space before or after"

 

 

 

 

 

 

 

Link to comment
Share on other sites

jcbones,

 

How about this...

 

// Check HTML Title.
if (empty($trimmed['htmlTitle'])){
$errors['htmlTitle'] = 'Please enter an HTML Title.';
}else{
if (strlen($trimmed['htmlTitle']) > 1 && strlen($trimmed['htmlTitle']) < 101){
	$htmlTitle = $trimmed['htmlTitle'];
}else{
	$errors['htmlTitle'] = 'HTML Title must be 2-100 characters';
}
}

 

If anyone wants to help me figure out how to do the same thing using a Regular Expression, that would be a bonus.

 

Thanks,

 

 

Debbie

 

 

Link to comment
Share on other sites

^[ \t]+|[ \t]+$ matches leading and trailing whitespace (TABS or SPACES )

 

that is if you don't want to strip whitespace and have something like

 

thisisacontinuouslinewithoutspacesortabs

 

 

then you would have "This is a line without a space before or after"

 

^[ \t]+|[ \t]+$ says...

 

one or more spaces or tabs OR one or more spaces or tabs

 

That doesn't make any sense?!

 

 

I can probably modify what I said earlier to...

 

"Allow any character on a U.S. keyboard and whitespaces, however the string cannot be all whitespaces and must be a length between 2 and 100 characters total."

 

 

Debbie

 

 

Link to comment
Share on other sites

i don't understand what whitespace you want to disallow :confused:

 

What you have...

 

^[ \t]+|[ \t]+$

says...

 

one or more spaces or tabs OR one or more spaces or tabs

 

It doesn't say anything about allowing for Non-Whitespace Characters.

 

 

or how your whole idea would work and what i said DOES make since it would strip whitespaces  and tabs before or after a string.

 

If I have a form field called "Article Title", then while I will allow spaces, I don't want someone to enter 100 spaces as their entire entry.

 

The suggestions above (e.g. "\s" or "\s\S") would do that.

 

Valid Article Titles:

"Postage Meters can save your business money!"

 

"Why communicating online is sooo difficult sometimes?!"

 

"Moving towards answers at a snail's pace!!!"

 

 

Invalid Article Titles:

" "

" "

 

 

Debbie

 

 

Link to comment
Share on other sites

So use:

$variable = trim($variable);
$length = strlen($variable);
if($length > 1 && $length < 101) {
  //do something
}

 

You are trying to use a function, for no other reason than you WANT to use it.  Even though it would be using un-needed overhead.

 

1.) Why is using a Regex "un-needed overhead"?

 

2.) No, I'm trying to learn how to do something multiple ways.

 

3.) I prefer Regex because I can do in one line what code usually takes several likes and often isn't as thorough.

 

So, now I know how to do what I want using PHP functions and code, but I'd still be curious to see how to do this using Regex, because it seems like a very common need?!

 

 

Debbie

 

I ran a regex on a string containing 93 characters, and then ran the same string through strlen AND trim.

Here is the results.

This is a string that contains more than 2 characters but less than 100 characters in length.

10,000 runs of preg_match takes 0.0234 seconds

10,000 runs of strlen() and trim() takes 0.0096 seconds.

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.