PHP/String/String Matches
Содержание
$: anchors a pattern to the end of a line or the end of the string
<?php
$string = " is PHP";
$pattern = "/PHP$/";
if(preg_match($pattern, $string))
print("Found a match!");
?>
A simple use of preg_match()
<?php
$string = "PHP is the web scripting language";
$pattern = "/PHP/";
if(preg_match($pattern, $string))
print("Found a match!");
?>
String match and if statement
<?php
$string = "numbers 42";
$pattern = "/[^0-9]$/";
if(preg_match($pattern, $string))
print("Found a match!");
?>
String match demo: |
<?php
$string = "Cats coat dog date dig daughter catagory";
$pattern = "/cat|dog/";
if(preg_match($pattern, $string))
print("Found a match!");
?>
String match demo: /[cd][oa][gt]/
<?php
$string = "Cats coat dog date dig daughter catagory";
$pattern = "/[cd][oa][gt]/";
if(preg_match($pattern, $string))
print("Found a match!");
?>
String matches: ^ (begine)
<?php
$string = "PHP is ";
$pattern = "/^PHP/";
if(preg_match($pattern, $string))
print("Found a match!");
?>
String matches: string start
<?php
$pattern = "/^PHP$/";
$string = " PHP ";
if(preg_match($pattern, $string)){
print("Found a match!");
}
$string = "PHP is ";
if(preg_match($pattern, $string)){
print("This line won"t print!");
}
?>
String match for domain name
<?
function simple_dot_com ($url) {
return(ereg("^www\.[a-z]+\.ru$", $url));
}
$urls_to_test =
array("www.ibm.ru", "www.java.sun.ru",
"www.zend.ru", "java.sun.ru",
"www.java.sun.ru", "www.php.net",
"www.IBM.ru",
"www.Web addresses can\"t have spaces.ru");
while($test = array_pop($urls_to_test)){
if (simple_dot_com($test))
print("\"$test\" is a simple dot-com<BR>");
else
print("\"$test\" is NOT a simple dot-com<BR>");
}
?>