PHP
<?PHP
$str="2000-08-12T03:54:54Z";
if (preg_match("/\d/",$str))
{
echo "String contains number.";
}
?>
The matches can be stored into an array which is the 3rd parameter of the function.
<?PHP
$str="2000-08-12T03:54:54Z";
if (preg_match("/^((\d+)\-\d+\-\d+)T/",$str,$mats))
{
echo "$mats[0]"; //2000-08-12T
echo "$mats[1]"; //2000-08-12
echo "$mats[2]"; //2000
}
?>
<?PHP
$str="2000-08-12T03:54:54Z";
if (preg_match("/00|03/",$str))
{
echo "String contains \"03\" or \"00\"";
}
?>
Let's see another example, and print out the order of matches:
<?PHP
$str = "The date is 2014-04-14.";
if (preg_match("/\w+\s(\d{2}\-\d{2}-\d{4})|\w+\s(\d{4}\-\d{2}-\d{2})/", $str, $mats))
{
for ($ii=0;$ii<count($mats);$ii++)
{
echo "$ii: $mats[$ii]\n";
}
}
?>
The output is:
0: is 2014-04-14
1:
2: 2014-04-14
<?PHP
$str = "match Can Be Case Insensitive with i modifier";
if (preg_match("/can/i",$str)) echo "True"; else "False"; //True
?>
<?PHP
$str = "match Can Be Case Insensitive with i modifier";
echo preg_match_all("/se/",$str); //2
echo preg_match("/se/",$str); //1
?>
Let's list all the matches:
<?PHP
$str = "This is the website <a href=\"http://https://convert.idontcarewhatyouthink.net\">perschon</a> and google website <a href=\"http://www.google.com\">google</a>.";
if (preg_match_all("/\<a href=\"([^\>]+)\"\>([^\>]+)\<\/a\>/i",
$str, $mats, PREG_SET_ORDER) > 0)
{
for ($ii=0;$ii<count($mats);$ii++)
{
$groups = $mats[$ii];
$core = $groups[0];
$site = $groups[1];
$name = $groups[2];
echo "$ii: $core, $site, $name\n";
}
}
?>
The output is:
0: <a href="http://https://convert.idontcarewhatyouthink.net">perschon</a>, http://https://convert.idontcarewhatyouthink.net, perschon
1: <a href="http://www.google.com">google</a>, http://www.google.com, google
If
<?PHP $str = "This is the website perschon and google website google."; if (preg_match_all("/\^lt;a href=\"([^\>]+)\"\>([^\>]+)\<\/a\>/i", $str, $mats) > 0) { for ($ii=0;$ii<count($mats);$ii++) { $groups = $mats[$ii]; echo "$ii: "; for ($jj=0;$jj<count($groups);$jj++) { echo "$groups[$jj], "; } echo "\n"; } } ?>
The output is:
0: <a href="http://https://convert.idontcarewhatyouthink.net">perschon</a>, <a href="http://www.google.com">google</a>,
1: http://https://convert.idontcarewhatyouthink.net, http://www.google.com,
2: perschon, google,
Similiar functions include