preg_match

preg_match — Perform a regular expression match

Parameters:

pattern: The pattern to search for, as a string.

subject: The input string.

matches: If matches is provided, then it is filled with the results of search. $matches[0] will contain the text that matched the full pattern, $matches[1] will have the text that matched the first captured parenthesized subpattern, and so on.

flags: flags can be the following flag:

PREG_OFFSET_CAPTURE: If this flag is passed, for every occurring match the appendant string offset will also be returned. Note that this changes the value of matches into an array where every element is an array consisting of the matched string at offset 0 and its string offset into subject at offset 1.

offset: Normally, the search starts from the beginning of the subject string. The optional parameter offset can be used to specify the alternate place from which to start the search (in bytes).

<?php
    $subject = "abcdef";
    $pattern = '/^def/';
    preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE, 3);
    print_r($matches);
?>
The above example will output:
Array
(

)

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
0 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

options: i case insensitive m make dot match newlines x ignore whitespace in regex o perform #{…} substitutions only once

Example: 

$url = "http://prafulkr.in/autosuggest.php";

preg_match("/autosuggest.php/", $url']);

Posted in PHP | Tagged , , | Comments Off

Comments are closed.