Solved

ctype_alpha - how to add space

Posted on 2009-05-19
4
1,387 Views
Last Modified: 2012-05-07
j.e ( if ctype_alpha => true if there are white spaces inside)
function _surname($v) {

	if(!empty($v)) {

		if(!ctype_alpha($v)) $e .= '» Please use only alphabet(no digits) characters in surname (+ no spaces)'."\r";

		if(!lenght($v,2)) $e .= '» Surname must be above 2 letters lenght.'."\r";

	} return $e;

}

Open in new window

0
Comment
Question by:AndyPSV
  • 3
4 Comments
 
LVL 40

Expert Comment

by:RQuadling
ID: 24431357
I don't think you can alter ctype.

But a regex will do ...

if (0 == preg_match('`^^[a-z](?!.*  .*)[a-z ]+[a-z]$`i', $v)) $e .= '» Please use only alphabet(no digits) characters in surname (+ spaces)'."\r";

The regex is saying must start and end with a letter and may contain spaces and letters but not 2 spaces next to each other.


0
 

Author Comment

by:AndyPSV
ID: 24438297
The regex is saying must start and end with a letter and may contain spaces and letters but not 2 spaces next to each other.

How to modify this to trim the empty 2 spaces to one?
0
 
LVL 40

Accepted Solution

by:
RQuadling earned 500 total points
ID: 24439036
Something like this (and sorry for the typo of the double ^. Cut and paste error).

if (0 == preg_match('`^(?! )[a-z ]+(?<! )$`i', preg_replace('` +`', ' ', trim($v, ' ')), $a_Matches))

and then use $a_Matches[0] as the new cleaned value of $v


Below is a usage test.

The output is ...

' '                                     : Failed to pass test.
'  '                                    : Failed to pass test.
'       starts with spaces'             : Passed test and was returned as 'starts with spaces'.
'ends with spaces         '             : Passed test and was returned as 'ends with spaces'.
'      wrapped with spaces          '   : Passed test and was returned as 'wrapped with spaces'.
'one space'                             : Passed test and was returned as 'one space'.
'nospace'                               : Passed test and was returned as 'nospace'.
'lots    of    spaces'                  : Passed test and was returned as 'lots of spaces'.


<?php

$a_Tests = array

	(

	' ',

	'  ',

	'       starts with spaces',

	'ends with spaces         ',

	'      wrapped with spaces          ',

	'one space',

	'nospace',

	'lots    of    spaces'

	);
 

foreach($a_Tests as $s_Test)

	{

	echo str_pad("'$s_Test' ", 40) . ": ";

	if (0 == preg_match('`^(?! )[a-z ]+(?<! )$`i', preg_replace('` +`', ' ', trim($s_Test, ' ')), $a_Matches))

		{

		echo "Failed to pass test.", PHP_EOL;

		}

	else

		{

		$s_Test = $a_Matches[0];

		echo "Passed test and was returned as '$s_Test'.", PHP_EOL;

		}

	}

Open in new window

0
 
LVL 40

Expert Comment

by:RQuadling
ID: 24448505
Slight update. The regex can be simplified. See comments in the code below.
<?php

$a_Tests = array

        (

        ' ',

        '  ',

        '       starts with spaces',

        'ends with spaces         ',

        '      wrapped with spaces          ',

        'one space',

        'nospace',

        'lots    of    spaces'

        );

 

foreach($a_Tests as $s_Test)

        {

        echo str_pad("'$s_Test' ", 40) . ": ";

	// As we are trimming the string, no leading or trailing spaces.

	// As we replace 2+ spaces with 1 only single spaces.

	// So, the regex can be a lot simpler. Must match at least 1 letter or space

	// (though 1 space will never actually get to the regex test).

	// If you want to say there has to be at least 10 letters, use a regex of ...

	// '`^[a-z ]{10,}$`i'

	// The {10,} means at least 10 lettes.

	// If you want between 10 and 40 letters ...

	// '`^[a-z ]{10,40}$`i'

        if (0 == preg_match('`^[a-z ]+$`i', preg_replace('` +`', ' ', trim($s_Test, ' ')), $a_Matches))

                {

                echo "Failed to pass test.", PHP_EOL;

                }

        else

                {

                $s_Test = $a_Matches[0];

                echo "Passed test and was returned as '$s_Test'.", PHP_EOL;

                }

        }

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now