PHP handleiding

PHP TUIS PHP Inleiding PHP installeer PHP sintaksis PHP opmerkings PHP veranderlikes PHP Echo / Druk PHP datatipes PHP Strings PHP-nommers PHP Wiskunde PHP konstante PHP-operateurs PHP As...Anders...Elseif PHP skakelaar PHP-lusse PHP funksies PHP-skikkings PHP Superglobals PHP RegEx

PHP- vorms

PHP-vormhantering PHP-vorm validering PHP-vorm word vereis PHP-vorm URL/e-pos PHP-vorm voltooi

PHP Gevorderd

PHP datum en tyd PHP sluit in PHP-lêerhantering PHP-lêer oop/lees PHP-lêer skep/skryf PHP-lêer oplaai PHP-koekies PHP-sessies PHP filters PHP-filters Gevorderd PHP-terugbelfunksies PHP JSON PHP-uitsonderings

PHP OOP

PHP Wat is OOP PHP Klasse/Objekte PHP Konstrukteur PHP vernietiger PHP Toegangswysigers PHP erfenis PHP konstante PHP Abstrakte Klasse PHP-koppelvlakke PHP eienskappe PHP statiese metodes PHP Statiese Eienskappe PHP naamruimtes PHP Iterables

MySQL- databasis

MySQL-databasis MySQL Connect MySQL Skep DB MySQL Skep tabel MySQL Voeg data in MySQL Kry Laaste ID MySQL Voeg veelvuldige in MySQL voorberei MySQL Kies Data MySQL Waar MySQL Bestel deur MySQL verwyder data MySQL-opdateringsdata MySQL-limietdata

PHP XML

PHP XML-ontleders PHP SimpleXML-ontleder PHP SimpleXML - Kry PHP XML Expat PHP XML DOM

PHP - AJAX

AJAX Intro AJAX PHP AJAX-databasis AJAX XML AJAX Live Search AJAX-peiling

PHP voorbeelde

PHP voorbeelde PHP samesteller PHP vasvra PHP Oefeninge PHP-sertifikaat

PHP- verwysing

PHP Oorsig PHP Skikking PHP-kalender PHP datum PHP gids PHP fout PHP-uitsondering PHP lêerstelsel PHP filter PHP FTP PHP JSON PHP sleutelwoorde PHP Libxml PHP-pos PHP Wiskunde PHP Diverse PHP MySQLi PHP-netwerk PHP-uitsetbeheer PHP RegEx PHP SimpleXML PHP-stroom PHP-string PHP veranderlike hantering PHP XML-ontleder PHP zip PHP Tydsones

PHP Gereelde Uitdrukkings


Wat is 'n gereelde uitdrukking?

'n Gereelde uitdrukking is 'n reeks karakters wat 'n soekpatroon vorm. Wanneer jy na data in 'n teks soek, kan jy hierdie soekpatroon gebruik om te beskryf waarna jy soek.

'n Gereelde uitdrukking kan 'n enkele karakter wees, of 'n meer ingewikkelde patroon.

Gereelde uitdrukkings kan gebruik word om alle soorte tekssoektogte en teksvervangingsbewerkings uit te voer.


Sintaksis

In PHP is gereelde uitdrukkings stringe wat bestaan ​​uit skeidingstekens, 'n patroon en opsionele wysigers.

$exp = "/w3schools/i";

In die voorbeeld hierbo, /is die afbakener , w3schools is die patroon waarna gesoek word, en iis 'n wysiger wat die soektog hoofletter-onsensitief maak.

Die skeidingsteken kan enige karakter wees wat nie 'n letter, syfer, agterste skuinsstreep of spasie is nie. Die mees algemene skeidingsteken is die voorwaartse skuinsstreep (/), maar wanneer jou patroon voorwaartse skuinsstrepe bevat, is dit gerieflik om ander skeidingstekens soos # of ~ te kies.


Gereelde uitdrukkingsfunksies

PHP bied 'n verskeidenheid funksies wat jou toelaat om gereelde uitdrukkings te gebruik. Die preg_match(), preg_match_all()en preg_replace()funksies is van die mees gebruikte:

Function Description
preg_match() Returns 1 if the pattern was found in the string and 0 if not
preg_match_all() Returns the number of times the pattern was found in the string, which may also be 0
preg_replace() Returns a new string where matched patterns have been replaced with another string

Gebruik preg_match()

Die preg_match()funksie sal jou vertel of 'n string passings van 'n patroon bevat.

Voorbeeld

Gebruik 'n gewone uitdrukking om 'n hoofletter-onsensitiewe soektog vir "w3schools" in 'n string te doen:

<?php
$str = "Visit W3Schools";
$pattern = "/w3schools/i";
echo preg_match($pattern, $str); // Outputs 1
?>

Gebruik preg_match_all()

Die preg_match_all()funksie sal jou vertel hoeveel passings gevind is vir 'n patroon in 'n string.

Voorbeeld

Gebruik 'n gereelde uitdrukking om 'n hoofletter-onsensitiewe telling van die aantal gevalle van "ain" in 'n string te doen:

<?php
$str = "The rain in SPAIN falls mainly on the plains.";
$pattern = "/ain/i";
echo preg_match_all($pattern, $str); // Outputs 4
?>

Gebruik preg_replace()

Die preg_replace()funksie sal al die passings van die patroon in 'n string vervang met 'n ander string.

Voorbeeld

Gebruik 'n hoofletter-onsensitiewe gereelde uitdrukking om Microsoft met W3Schools in 'n string te vervang:

<?php
$str = "Visit Microsoft!";
$pattern = "/microsoft/i";
echo preg_replace($pattern, "W3Schools", $str); // Outputs "Visit W3Schools!"
?>


Gereelde uitdrukking wysigers

Wysigers kan verander hoe 'n soektog uitgevoer word.

Modifier Description
i Performs a case-insensitive search
m Performs a multiline search (patterns that search for the beginning or end of a string will match the beginning or end of each line)
u Enables correct matching of UTF-8 encoded patterns

Gereelde uitdrukkingspatrone

Hakies word gebruik om 'n reeks karakters te vind:

Expression Description
[abc] Find one character from the options between the brackets
[^abc] Find any character NOT between the brackets
[0-9] Find one character from the range 0 to 9

Meta-karakters

Metakarakters is karakters met 'n spesiale betekenis:

Metacharacter Description
| Find a match for any one of the patterns separated by | as in: cat|dog|fish
. Find just one instance of any character
^ Finds a match as the beginning of a string as in: ^Hello
$ Finds a match at the end of the string as in: World$
\d Find a digit
\s Find a whitespace character
\b Find a match at the beginning of a word like this: \bWORD, or at the end of a word like this: WORD\b
\uxxxx Find the Unicode character specified by the hexadecimal number xxxx

Kwantifiseerders

Kwantifiseerders definieer hoeveelhede:

Quantifier Description
n+ Matches any string that contains at least one n
n* Matches any string that contains zero or more occurrences of n
n? Matches any string that contains zero or one occurrences of n
n{x} Matches any string that contains a sequence of X n's
n{x,y} Matches any string that contains a sequence of X to Y n's
n{x,} Matches any string that contains a sequence of at least X n's

Let wel: As jou uitdrukking vir een van die spesiale karakters moet soek, kan jy 'n terugskuinsstreep ( \ ) gebruik om hulle te ontsnap. Byvoorbeeld, om vir een of meer vraagtekens te soek kan jy die volgende uitdrukking gebruik: $pattern = '/\?+/';


Groepering

Jy kan hakies ( )gebruik om kwantifiseerders op hele patrone toe te pas. Hulle kan ook gebruik word om dele van die patroon te kies om as 'n vuurhoutjie te gebruik.

Voorbeeld

Gebruik groepering om na die woord "piesang" te soek deur na ba te soek, gevolg deur twee gevalle van na :

<?php
$str = "Apples and bananas.";
$pattern = "/ba(na){2}/i";
echo preg_match($pattern, $str); // Outputs 1
?>

Voltooi RegExp-verwysing

Vir 'n volledige verwysing, gaan na ons volledige PHP Regular Expression Reference .

Die verwysing bevat beskrywings en voorbeelde van alle Reguliere uitdrukking-funksies.