PHP/String/preg match all
Содержание
- 1 A non-capturing optional subpattern
- 2 Capturing HTML headings
- 3 Extracting text from HTML tags
- 4 Finding Matches Globally with preg_match_all()
- 5 Finding the nth match
- 6 Making a quantifier match as few characters as possible
- 7 Matching with preg_match_all()
- 8 m modifier: match an anchored pattern on multiple lines of text.
- 9 preg_match_all
- 10 preg_match_all demo
- 11 preg_match_all() function matches all occurrences of pattern in string.
- 12 preg_match_all searches a string for all the occurrences of a regular expression
- 13 Reading Records with a Delimiter
- 14 Using preg functions
- 15 Using preg_match_all() to Match a Pattern Globally
A non-capturing optional subpattern
<?php
$html = "<link rel="icon" href="http://www.example.ru/icon.gif"/>
<link rel="prev" title="Previous" href="http://www.example.ru/prev.xml"/>
<link rel="next" href="http://www.example.ru/next.xml"/>";
preg_match_all("/rel="(?:prev|next)"(?: title="[^"]+?")? href=
"([^"]*?)"/", $html, $linkMatches);
print "$bothMatches is: "; var_dump($linkMatches);
?>
Capturing HTML headings
<?php
$html = file_get_contents("example.html");
preg_match_all("@<h([1-6])>(.+?)</h\1>@is", $html, $matches);
foreach ($matches[2] as $text) {
print "Heading: $text \n";
}
?>
Extracting text from HTML tags
<?php
$html = file_get_contents("example.html");
preg_match_all("@<(strong|em)>(.+?)</\1>@is", $html, $matches);
foreach ($matches[2] as $text) {
print "Text: $text \n";
}
?>
Finding Matches Globally with preg_match_all()
<?
$text = "take, tart, till";
if ( preg_match( "/\bt\w+s\b/", $text, $array ) ) {
print "<pre>\n";
print_r( $array );
print "</pre>\n";
}
?>
Finding the nth match
<?php
$todo = "1. a 2. B 3. C";
preg_match_all("/\d\. ([^\d]+)/", $todo, $matches);
print "The second item on the todo list is: ";
print $matches[1][1];
print "The entire todo list is: ";
foreach($matches[1] as $match) {
print "$match\n";
}
?>
Making a quantifier match as few characters as possible
<?php
// find all <em>emphasized</em> sections
preg_match_all("@<em>.+?</em>@", $html, $matches);
?>
Matching with preg_match_all()
<?
$html = <<<_HTML_
<ul>
<li>BeefFun</li>
<li>Pea</li>
<li>Noodles</li>
</ul>
_HTML_;
preg_match("@<li>(.*?)</li>@",$html,$matches);
$match_count = preg_match_all("@<li>(.*?)</li>@",$html,$matches_all);
print "preg_match_all() matched $match_count times.\n";
print "preg_match() array: ";
var_dump($matches);
print "preg_match_all() array: ";
var_dump($matches_all);
?>
m modifier: match an anchored pattern on multiple lines of text.
The anchor patterns ^ and $ match the beginning and end of an entire string by default.
<?
$text = "name: Joe\noccupation: coder\n\n";
if ( preg_match_all( "/^\w+:\s+(.*)$/m", $text, $array ) ) {
print "<pre>\n";
print_r( $array );
print "</pre>\n";
}
?>
preg_match_all
<?php
$userinfo = "Name: <b>PHP</b> <br> Title: <b>Programming Language</b>";
preg_match_all ("/<b>(.*)<\/b>/U", $userinfo, $pat_array);
print $pat_array[0][0]." <br> ".$pat_array[0][1]."\n";
?>
preg_match_all demo
<?
$a = "Foo moo boo tool foo!";
preg_match_all("/[A-Za-z]oo\b/i", $a, $matches);
var_dump($myarray);
?>
preg_match_all() function matches all occurrences of pattern in string.
Its syntax: int preg_match_all (string pattern, string string, array pattern_array [, int order])
There are two possible types of order:
PREG_PATTERN_ORDER is the default.
PREG_PATTERN_ORDER: $pattern_array[0] is an array of all complete pattern matches, $pattern_array[1] is an array of all strings matching the first parenthesized regexp, and so on.
PREG_SET_ORDER: $pattern_array[0] contains elements matched by the first parenthesized regexp, $pattern_array[1] contains elements matched by the second parenthesized regexp, and so on.
<?
$userinfo = "Name: <b>R</b> <br> Title: <b>PHP</b>";
preg_match_all ("/<b>(.*)<\/b>/U", $userinfo, $pat_array);
print $pat_array[0][0]." <br> ".$pat_array[0][1]."\n";
?>
preg_match_all searches a string for all the occurrences of a regular expression
<?php
$s = "A beautiful day";
preg_match_all ("/beaut[^ ]+/", $s, $matches);
var_dump ($matches)
?>
Reading Records with a Delimiter
<?php
$file = fopen("testfile.csv", "r") or die("Cannot open file!\n");
while ($line = fgets($file, 1024)) {
preg_match_all("/[^,\"]+|\"([^\"]|\"\")*\"/", $line, $fields);
echo "First field is: " . $fields[0][0] . "\n";
echo "Second field is: " . $fields[0][1] . "\n";
}
fclose($file);
?>
Using preg functions
<?php
if (preg_match("{<title>.+</title>}", $html)) {
// page has a title
}
if (preg_match_all("/<li>/", $html, $matches)) {
print "Page has " . count($matches[0]) . " list items\n";
}
// turn bold into italic
$italics = preg_replace("/(<\/?)b(>)/", "$1i$2", $bold);
?>
Using preg_match_all() to Match a Pattern Globally
<html>
<head>
<title>Using preg_match_all() to Match a Pattern Globally</title>
</head>
<body>
<?php
$text = "plants, pianos and parrots";
if ( preg_match_all( "/\bp\w+s\b/", $text, $array ) ) {
print "<pre>\n";
print_r( $array );
print "</pre>\n";
}
?>
</body>
</html>