Quantcast
Channel: THOMAS.DEULING.ORG » REGULAR EXPRESSION
Viewing all articles
Browse latest Browse all 2

PHP Regular Expression to validate strings

$
0
0

Hi at all ;)

Since a long time i’m searching for a regex in PHP, that filter some letters as sample names. But the solutions that i’ve found could not filter all letters i need.
The most ones didn’t gets letters like à, ä, æ, and so on, without including the letters explicit in the regex.

These week Lars, my college from iscope ;) , founds the following solution to take all Unicode characters:

<?php
class Validate {
	
	/**
	 * Checks a String
	 *
	 * @author     Thomas Deuling <tdeuling@gmail.com>
	 *             http://thomas.deuling.org
	 * @param      string $value String to check
	 * @return     boolean Is valid?!
	 */
	public static function lettersOnly($value="") {
		if (!preg_match("/^[\pL]*$/", $value)) return false;
		return true;
	}	
	
	...
	
}
?>

Here some test cases:

<?php
require_once("./validate.class.php");

$testvalue = "gfgsergds";
$function = "lettersOnly";
echo "Validate::".$function."(".$testvalue."): ".(string)Validate::$function($testvalue)."<br>";
// Validate::lettersOnly(gfgsergds): 1

$testvalue = "gfs3467rgds";
$function = "lettersOnly";
echo "Validate::".$function."(".$testvalue."): ".(string)Validate::$function($testvalue)."<br>";
// Validate::lettersOnly(gfs3467rgds):

$testvalue = "g%/)=gsgds";
$function = "lettersOnly";
echo "Validate::".$function."(".$testvalue."): ".(string)Validate::$function($testvalue)."<br>";
// Validate::lettersOnly(g%/)=gsgds):

$testvalue = utf8_decode("aäüöføæhgfd");
$function = "lettersOnly";
echo "Validate::".$function."(".$testvalue."): ".(string)Validate::$function($testvalue)."<br>";
// Validate::lettersOnly(aäüöføæhgfd): 1

?>

Viewing all articles
Browse latest Browse all 2

Trending Articles