Regular Expressions Tutorial: Visit
Regular Expressions Tutorial: Visit
Regular Expressions Tutorial: Visit
A Cure
Regular Expressions
Tutorial
I have searched the web far and near for a
good tutorial on PHP Regular Expressions
and I have come up with a multitude of sites.
However, I needed just a little bit of
information from each of the sites and I ended
up trying to move between 10 different
webpages to get the information I needed at a
particular time. This tutorial is a
collation of all those bits of information.
Some of this is my work, but it is mostly
good collection of other tutorials available
out there. In order to give authors credit
for their work, I have included ALL the links
of those pages and if anyone feels like
this is an outrage, let me know and I will
take down the relevant information.
So here goes...
Advertise Here
PHPBuilder.com)
First of all, let's take a look at two special
symbols: '^' and '$'. What they do is indicate
the
start and the end of a string, respectively,
like this:
* /<\/\w+>/
* |(\d{3})-\d+|Sm
* /^(?i)php[34]/
* {^\s+(\s+)?$}
preg_split
PREG_SPLIT_NO_EMPTY
If this flag is set, only non-empty pieces
will be returned by preg_split().
PREG_SPLIT_DELIM_CAPTURE
If this flag is set, parenthesized
expression in the delimiter pattern will be
captured and
returned as well. This flag was added for
4.0.5.
PREG_SPLIT_OFFSET_CAPTURE
If this flag is set, for every occuring
match the appendant string offset will also be
returned. Note that this changes the return
value in an array where every element is an
array consisting of the matched string at
offset 0 and it's string offset into subject
at offset 1. This flag is available since PHP
4.3.0 .
<?php
// split the phrase by any number of commas or
space characters,
// which include " ", \r, \t, \n and \f
$keywords = preg_split ("/[\s,]+/", "hypertext
language, programming");
?>
<?php
$str = 'hypertext language programming';
$chars = preg_split('/ /', $str, -1,
PREG_SPLIT_OFFSET_CAPTURE);
print_r($chars);
?>
will yield:
Array
(
[0] => Array
(
[0] => hypertext
[1] => 0
)
preg_match
PREG_OFFSET_CAPTURE
If this flag is set, for every occuring
match the appendant string offset will also
be returned. Note that this changes the
return value in an array where every element
is an array consisting of the matched string
at offset 0 and it's string offset into
subject at offset 1. This flag is available
since PHP 4.3.0 .
<?php
// The "i" after the pattern delimiter indicates
a case-insensitive search
if (preg_match ("/php/i", "PHP is the web
scripting language of choice.")) {
print "A match was found.";
} else {
print "A match was not found.";
}
?>
<?php
/* The \b in the pattern indicates a word
boundary, so only the distinct
* word "web" is matched, and not a word partial
like "webbing" or "cobweb" */
if (preg_match ("/\bweb\b/i", "PHP is the web
scripting language of choice.")) {
print "A match was found.";
} else {
print "A match was not found.";
}
<?php
// get host name from URL
preg_match("/^(http:\/\/)?([^\/]+)/i",
"http://www.php.net/index.html", $matches);
$host = $matches[2];
crazygrrl.com)
When using Perl-style matching, the pattern also
has to be enclosed by special delimiters.
The default is the forward slash, though you can
use others. For example:
/colou?r/
!/root/home/random!
i to the pattern:
/colou?r/i
list):
Example:
/\bhomer\b/
Example:
$pattern = "/\b(do(ugh)?nut)\b.*\b(Homer|
Fred)\b/i";
print("<P>Match: $reg[0]</P>");
print("<P>Pastry: $reg[1]</P>");
print("<P>Variant: $reg[2]</P>");
print("<P>Name: $reg[3]</P>");
else {
print("No match.");
Results:
Match: donut, Homer
Pastry: donut
Name: Homer
case-insensitive.
<?
function valid_ipv4($ip_addr)
$num="([0-9]|1?\d\d|2[0-4]\d|25[0-5])";
$range="([1-9]|1\d|2\d|3[0-2])";
if(preg_match("/^$num\.$num\.$num\.
$num(\/$range)?$/",$ip_addr))
return 1;
}
return 0;
$ip_array[] = "127.0.0.1";
$ip_array[] = "127.0.0.256";
$ip_array[] = "127.0.0.1/36";
$ip_array[] = "127.0.0.1/1";
if(valid_ipv4($ip_addr))
else
{
echo "$ip_addr is NOT
valid<BR>\n";
?>
$sPREVIOUSHANDLER = Set_Error_Handler
("TrapError");
Preg_Match ($sREGEXP, "");
Restore_Error_Handler ($sPREVIOUSHANDLER);
Function TrapError ()
Static $iERRORES;
If (!Func_Num_Args ())
$iRETORNO = $iERRORES;
$iERRORES = 0;
Return $iRETORNO;
Else
{
$iERRORES++;
<?php
function get_title_tag($chaine){
break;
}
return $out[1];
else{
return false;
?>
<?php
$referer = $_SERVER['HTTP_REFERER'];
if (file_exists($filename)){
$lastvisit = filectime($filename);
$currentdate = date('U');
$difference = round(($currentdate -
$lastvisit)/84600);
if ($difference > 7) {
unlink($filename);
if (!$_SERVER['HTTP_REFERER']) $url_test =
'http://dinki.mine.nu/weblog/';
$new_name = stripslashes("<beg>$new_title\n");
$new_URL = stripslashes("<beg>$referer\n");
fwrite($fp,$new_URL);
fwrite($fp,$new_name);
fclose($fp);
$foo = preg_split("/<beg>/",$file);
$number = count($foo);
//print $number;
fclose($fp);
$print1 = $foo[$count];
$print2 = $foo[$count+1];
print "<a
href=$print1>$print2</a>"; //print $count;
$count += 2;
$new_name =
stripslashes("<beg>$print2");
$new_URL =
stripslashes("<beg>$print1");
fwrite($fp,$new_URL);
fwrite($fp,$new_name);
fclose($fp);
//print_r($foo);
else {
$count = 1;
$print1 = $foo[$count];
$print2 = $foo[$count+1];
print "<a
href=$print1>$print2</a>"; //print $count;
$count += 2;
fclose($fp);
function return_title($url) {
if
(preg_match("/<title>(.*)<\/title>/i",
$array[$i], $tag_contents)) {
$title =
$tag_contents[1];
$title =
strip_tags($title);
return $title;
?>
35236
page 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
page 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1