Training

"Winners make choices,
losers make excuses.
"
Decide to be a Winner!!!!

±Arrows Getting Started

± Phase 1
Planning

± Website Tools

± Phase 3
Conceptual Design

± Helpful Information

± Phase 4
Physical Design

± Phase 5
Testing

± Phase 6
Implement and Market Website

± Other Web Development Items

± Multimedia

± Useful Utilities

± Programming

± Advanced Programming

± Microsoft Office Products

± Computer Maintenance

± Other


Web Design

NOTE: This is a collection of information and links collected over the years that might provide useful information. A Safer Company LLC does not guarantee, endorse or approve any of these links or their scripts. Use these links to other websites at your own risk.

Regular Expression - Regex

A regular expression is a pattern describing a certain amount of text. It is also called regex.

 
A way of describing a pattern in text - for example:
  • all the words that begin with the letter
  • every 10-digit phone number
  • every sentence with two commas in it, and no capital letter Q

Check for a phone number pattern:

     /^([1]-)? [0-9]{3}- [0-9]{3}- [0-9]{4} $/i   
  • slashes (/) at the start and end are the delimiters that signal where the pattern starts and ends.
  • caret (^) indicates the beginning of the string we want to check
  • ([1]-)? is asking “is there a ’1′ followed by a hyphen. The ? question mark at the end makes this portion optional. In other words, if it’s not there — meaning that it’s not an 800 number — it won’t immediately assume that the phone number is invalid.
  • [0-9]{3}- is looking for ONLY numbers [0-9] and there must be three {3} of them. Now, the added hyphen after the {3}- makes it so that hyphens are required in the phone number.
  • [0-9]{3}- is doing the same thing — making sure there are three numbers and a hyphen. Now, if you will notice, the last of these ends with {4}. As you my have guessed, this requires there to be four digits in the last part of the number.
  • The $ at end signifies that this is the end of our string.
  • i after the final slash (/i) is a switch that tells regex to perform a case-insensitive search. In other words “aaa” would be considered the same as “AAA”. This isn’t too important when validating a phone number, but you should definitely include it when validating an email address.

Check for email pattern:

/^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$/i  
  • amount of letters or numbers
  • followed by the @ symbol
  • followed by any letters or numbers (the domain name)
  • followed by a .
  • and then ending with any combination of 2 or 3 letters

Find a Phrase in a Phrase

<?php
// The "i" after the pattern delimiter indicates a case-insensitive search
if (preg_match("/php/i", "PHP is the web scripting language of choice.")) {
echo "A match was found.";
 } else {
 echo "A match was not found.";
 }
 ?>

Find a Word in a Phrase

<?php
/* The \b in the pattern indicates a word boundary, so only the distinct word "web" is matched, 
and not a word partial like "webbing" or "cobweb" */
 if (preg_match("/\bweb\b/i", "PHP is the web scripting language of choice.")) {
 echo "A match was found.";
 } else {
 echo "A match was not found.";
 }
 

Using named subpattern

<?php
     $str = 'foobar: 2008';
     preg_match('/(?P<name>\w+): (?P<digit>\d+)/', $str, $matches);
     /* This also works in PHP 5.2.2 (PCRE 7.0) and later, however 
     * the above form is recommended for backwards compatibility */
     // preg_match('/(?<name>\w+): (?<digit>\d+)/', $str, $matches);
     
     print_r($matches);
     
     ?>

The above example will output:

Array  (      
		[0] => foobar: 2008      
		[name] => foobar      
		[1] => foobar      
		[digit] => 2008      
		[2] => 2008  
)  

Learning Regex

Links

Regular Expression Will match...
foo The string "foo"
^foo "foo" at the start of a string
foo$ "foo" at the end of a string
^foo$ "foo" when it is alone on a string
[abc] a, b, or c
[a-z] Any lowercase letter
[^A-Z] Any character that is not a uppercase letter
(gif|jpg) Matches either "gif" or "jpeg"
[a-z]+ One or more lowercase letters
[0-9\.\-] Аny number, dot, or minus sign
^[a-zA-Z0-9_]{1,}$ Any word of at least one letter, number or _
([wx])([yz]) wy, wz, xy, or xz
[^A-Za-z0-9] Any symbol (not a number or a letter)
([A-Z]{3}|[0-9]{4}) Matches three letters or four numbers

 

top of page

Page last updated: May 31, 2012 14:46 PM

It is all about:
Content and Navigation...

Web Site by: A Safer Company LLC