preg match si preg_match_all - exemple


Expresiile regulate, reprexinta, un tool foarte puternic prin care, pe baza unor reguli, se pot gasi si inlocui, parti din text. Mai jos aveti lista de reguli si ce reprezinta ele si mai multe exemple utile prin care se poate intelege fiecare ce face. 

Regex quick reference

[abc]        A single character: a, b or c
[^abc]      Any single character but a, b, or c
[a-z]         Any single character in the range a-z
[a-zA-Z]   Any single character in the range a-z or A-Z
^      Start of line
$     End of line
A     Start of string
z     End of string
.     Any single character
s     Any whitespace character
S     Any non-whitespace character
d     Any digit
D     Any non-digit
w     Any word character (letter, number, underscore)
W     Any non-word character
b     Any word boundary character
(...)     Capture everything enclosed
(a|b)     a or b
a?     Zero or one of a
a*     Zero or more of a
a+     One or more of a
a{3}     Exactly 3 of a
a{3,}     3 or more of a
a{3,6}     Between 3 and 6 of a
Special Sequences
w - matches any single character classified as a “word” character (alphanumeric or “_”)
W - matches any non-“word” character
s - Whitespace (space, tab CRLF)
S - Any non whitepsace character
d - Digits (0-9)
D - Any non digit character
Meta Characters
^ - Start of subject (or line in multiline mode)
$ - End of subject (or line in multiline mode)
. - Any character except new line
* - 0 or more times
+ - one or more times; shortest match
| - alternative
() - grouping; "storing"
[] - set of characters
{} - repetition modifier
- quote or special
n* - Zero or more of n
n+ - One or more of n
n? - Zero or one occurrences of n
{n} - n occurrences exactly
{n,} - At least n occurrences
{n,m} - Between n and m occurrences (inclusive)
Pattern Modifiers
i - Case Insensitive
m - Multiline mode - ^ and $ match start and end of lines
s - Dotall - . class includes newline
x - Extended– comments and whitespace
e - preg_replace only – enables evaluation of replacement as PHP code
S - Extra analysis of pattern
U - Pattern is ungreedy
u - Pattern is treated as UTF-8
Point based assertions
b - Word boundary
B - Not a word boundary
A - Start of subject
Z - End of subject or newline at end
z - End of subject
G - First matching position in subject
(?=) - Positive look ahead assertion foo(?=bar) matches foo when followed by bar
(?!) - Negative look ahead assertion foo(?!bar) matches foo when not followed by bar
(?<=) - Positive look behind assertion (?<=foo)bar matches bar when preceded by foo
(?<!) - Negative look behind assertion (?<!foo)bar matches bar when not preceded by foo
(? >) - Once-only subpatterns (? >d+)bar Performance enhancing when bar not present
(?(x)) - Conditional subpatterns
(?(3)foo|fu)bar - Matches foo if 3rd subpattern has matched, fu if not
(?#) - Comment (?# Pattern does x y or z)
Mai multe exemple pe baza regulilor de mai sus:
$string = "What is my ip and my favorite address is ";

// check if string starts with "ha"
echo preg_match("/^Wh/",$string); // result 1
// check if string ends with "ip" - case insensitive
echo preg_match("/ip$/i",$string); // result 0
// match for exact string
echo preg_match("/^ip$/i",$string); //result 0
// match for empty string
echo preg_match("/^$/i",$string); //result 0
// match for string "http://"
echo preg_match("/http:///i",$string); //result 1
// search for a string that start's with w and finish with p
echo preg_match("/a.*d/i",$string); //result 1
// get domain from url
$string = " link";
preg_match_all( "/(http:\/\/)(www\.)?([a-z0-9-_]+).([a-z]+)/i", $string, $matches );
//get email addresses
$string = "If you want to get more info contact me at and you will recieve more details or at";
preg_match_all( "/([*+!.&$#¦\'\\%\/0-9a-z^_`{}=?~:-]+)@([a-z0-9]+\.)+([a-z0-9]){2,4}/i", $string, $matches );
//extract word and digit and insert it into an array with key names setted by me
$string = "foobar: 2008";
preg_match("/(?P<name>\w+): (?P<digit>\d)/",$string,$matches);
//get text between square brackets [This] is a [test] string, [eat] my [shorts]
$string = "[This] is a [test] string, [eat] my [shorts]";
//search and replace
$string = 'The quick brown fox jumped over the lazy dog.';
$patterns[0] = '/quick/';
$patterns[1] = '/brown/';
$patterns[2] = '/fox/';
$replacements[0] = 'bear';
$replacements[1] = 'black';
$replacements[2] = 'slow';
$stringReturn = preg_replace($patterns, $replacements, $string);
echo $stringReturn; //The bear black slow jumped over the lazy dog.
// This example strips excess whitespace from a string
$string = 'foo    o';
$return = preg_replace("/\s+/"," ",$string);
echo $return; // return foo o
//reorder data
$string = '100 Rs 50';
$pattern = '/(\d+).(\w+) (\d+)/i';
$replacement = '${1}.$3 $2';
echo preg_replace($pattern, $replacement, $string); //return 100.50 Rs
//remove comments from text
$source = "How are you? /* Fine! */ Are you here?";
$return = preg_replace('/(\/\*.*\*\/)/',"", $source);
echo $return; // return How are you?  Are you here?


0 comentarii

Spune-ti parerea!

Cod validare
Categorii Arhiva
Convertire CDR in PhotoshopAdaugat pe 26 Ianuarie 2011

Am nevoie de un webmaster pentru 2 site-uriAdaugat pe 12 Octombrie 2009

Diferente dintre include si require in phpAdaugat pe 23 Iulie 2013

PHP PDOAdaugat pe 04 Aprilie 2016

Redirect 301 de la url fara www la url cu wwwAdaugat pe 26 Ianuarie 2011

RESTAdaugat pe 04 Aprilie 2016

PHP - namespacesAdaugat pe 04 Martie 2014

Sunete pentru butoaneAdaugat pe 24 Februarie 2011

Version Control Systems (VCS)Adaugat pe 29 Iunie 2015

Procentul de vizitare pentru un anumit keywordAdaugat pe 02 August 2009