Jump to content

Recommended Posts

can someone help me with a code that will echo an error message if the text contains anything other than a-Z characters, remove any spaces in front or in back format all text (see below) and for Format the text casing, First letter cap, all other lowercase, and First letter cap if its after a character+space (i.e. New york character+space New York)

 

ex: "nEw  YOrk" -> "New York" (detected that there was more than 1 space, echo error)

ex: LAS VEGAS -> "Las Vegas"

ex: "  DULUTH" -> "Duluth"

Link to comment
https://forums.phpfreaks.com/topic/155984-solved-formating-text-case/
Share on other sites

<?php
$input = array("NeW 1yOrK NotValid", "NewW YoRK Te valid", "valid new yourk", "valid", "not$ valid", " DULUTH");

foreach ($input as $in) {
preg_match('~(^[a-z ]*+)~is', $in, $matches);
if (!empty($matches[1]) && $matches[1] == $in) {
	$in = ucwords(strtolower($in));
	$in = trim($in);
	echo $in . " was valid.<br />";
}else {
	echo "Invalid Characters in {$in}.<br />";
}
}
?>

 

I know the regex can be done in a better manner, but my regex is block is on today. Hopefully nrg_alpha comes along and fixes that part :) Till then, the above should work.

 

Output from the above:

Invalid Characters in NeW 1yOrK NotValid.
Neww York Te Valid was valid.
Valid New Yourk was valid.
Valid was valid.
Invalid Characters in not$ valid.
Duluth was valid.

If I understand this correctly.. if the string doesn't contain a-Z (and I think spaces should be included in this, otherwise entries like 'new york' would fail), then it is invalid.. otherwise, clean things up by trimming initial and trailing spaces, and make only the first letter of every word caps...

 

Borrowing from premiso's snippet:

 

$input = array("NeW 1yOrK NotValid", "NewW YoRK Te valid", "valid new yourk", "valid", "not$ valid", " DULUTH", "nEw    YorK");

foreach ($input as $in) {
if(preg_match('~^[a-z ]*+$~i', $in)){
	$in = ucwords(strtolower(trim($in)));
	echo 'VALID - ' . $in . "<br />\n";
} else {
	echo 'INVALID - ' . $in . "<br />\n";
}
}

 

Output:

INVALID - NeW 1yOrK NotValid
VALID - Neww York Te Valid
VALID - Valid New Yourk
VALID - Valid
INVALID - not$ valid
VALID - Duluth
VALID - New York

 

Is this what is wanted?

Whether this is a hit or miss, just a few notes to premiso...

 

In your pattern, you used the s modifer.. but since there is no dot_match_all, that modifier is not used / thus not necessary.. Your use of character class was correct.. and character classes by default take newlines into account (unless specified).

 

I omitted the (!empty($matches[1]) && $matches[1] == $in) part, since the entry must contain a-Z and spaces to remotely qualify, empty string would fail this.

 

Other than that, looks good by my understanding.

Hold on.. I think there are some problems with my snippet (regarding tabs by example... ) brb

 

EDIT - Ok, the pattern should probably be:

~^[a-z\s]*+$~i

 

I use \s instead of a literal space, just to make sure stuff like tabs are taken into account (just in case).

Hold on.. I think there are some problems with my snippet (regarding tabs by example... ) brb

 

EDIT - Ok, the pattern should probably be:

~^[a-z\s]*+$~i

 

I use \s instead of a literal space, just to make sure stuff like tabs are taken into account (just in case).

 

can you tell me if this will work /~^[\w\s]*+{3,25}$~i/ i was told that \w is for all word characters, and as you meantion \s for space then min 3chr 25max

Well in PCRE, \w is at the minimum a-zA-Z0-9_ so if you don't want to allow underscores (among other potential characters that might be included within \w depending on the locale, you will probably not want to use that. In my case, \w also includes stuff like exponents and characters that have accents.. something to consider.

 

Don't be afraid to test things out.. Come up with test strings that are meant to throw the kitchen sink if you will towards the preg pattern. If there's issues, you'll be more likely to find them.

 

EDIT - \s is not just for spaces or tabs.. it is all whitespace characters... this can be usually represented as [ \t\f\v\r\n] (even though PCRE doesn't support vertical tabs '\v')

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.