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 preg_match() Funksie

❮ PHP RegExp-verwysing

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);
?>

Definisie en gebruik

Die preg_match()funksie gee terug of 'n passing in 'n string gevind is.


Sintaksis

preg_match(pattern, input, matches, flags, offset)

Parameterwaardes

Parameter Description
pattern Required. Contains a regular expression indicating what to search for
input Required. The string in which the search will be performed
matches Optional. The variable used in this parameter will be populated with an array containing all of the matches that were found
flags Optional. A set of options that change how the matches array is structured:
  • PREG_OFFSET_CAPTURE - When this option is enabled, each match, instead of being a string, will be an array where the first element is a substring containing the match and the second element is the position of the first character of the substring in the input.
  • PREG_UNMATCHED_AS_NULL - When this option is enabled, unmatched subpatterns will be returned as NULL instead of as an empty string.
offset Optional. Defaults to 0. Indicates how far into the string to begin searching. The preg_match() function will not find matches that occur before the position given in this parameter

Tegniese besonderhede

Terugkeerwaarde: Wys 1 as 'n passing gevind is, 0 indien geen passings gevind is nie en onwaar as 'n fout voorgekom het
PHP weergawe: 4+
Veranderinglog: PHP 7.2 - Bygevoeg die PREG_UNMATCHED_AS_NULL-vlag

PHP 5.3.6 - Die funksie gee terug as die offset langer is as die lengte van die invoer

PHP 5.2.2 - Benoemde subpatrone kan die (?'naam') en (? <naam>) gebruik sintaksis bykomend tot die vorige (?P<naam>)

Meer voorbeelde

Voorbeeld

Gebruik PREG_OFFSET_CAPTURE om die posisie in die invoerstring te vind waarin die passings gevind is:

<?php
$str = "Welcome to W3Schools";
$pattern = "/w3schools/i";
preg_match($pattern, $str, $matches, PREG_OFFSET_CAPTURE);
print_r($matches);
?>

❮ PHP RegExp-verwysing