018_preg_match.php

<?php
//https://www.cainiaojc.com/php/php-regular-expressions.html 中文版正则表达式说明
$mail1 = "ayang.li@qq.com";
$partten = "/^\S+\.\S+\@\S+\.\S+/";
if (preg_match($partten, $mail1)) {
    echo "mail1 is valid" . PHP_EOL;
} else {
    echo "mail1 is invalid" . PHP_EOL;
}

$str1 = "yao taozi, putao-lizi";
$partten1 = "/[\s,\-]/";
print_r(preg_split($partten1, $str1));

echo preg_replace("/\S{5}\.\S{2}/", "amao", "ayang.li@qq.com") . PHP_EOL;
echo preg_replace("/\S{5,}\.\S{2,}/", "amao", "ayang.li@qq.com") . PHP_EOL;
// 之所以有问题是\S{5,}\. 用贪婪匹配匹配到了com前面的部分

print_r(preg_grep("/\d+/", [1, "a", 2]));

$str2 = "yao yaole yao daha";
$partten2 = "/yao/";

preg_match_all($partten2, $str2, $matches);
print_r($matches); // Array ( [0] => Array ( [0] => yao [1] => yao [2] => yao ) )

 

posted @ 2025-04-27 16:29  黑山老猫  阅读(3)  评论(0)    收藏  举报