PHP Basics 2 Regular Expressions

不言
Release: 2023-03-24 16:22:01
Original
1480 people have browsed it

这篇文章介绍的内容是关于php的基础 ,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下

<!-- PHP中的正则表达式 -->
<?php
//     定界符:
//     中间写正则
//     //
//     $$
//     %%
//     ^^
//     @@
//     ()
//     AA
//     include_once &#39;Class6.php&#39;;
    
    //原子通常指空格、点、特殊符号等;
//     特殊标识的原子
//   \d 匹配一个0-9
//   \D 除了0-9以外的所有字符
//   \w a-zA-Z0-9_
//   \W 除了0-9A-Za-z_以外的所有字符
//   \s 匹配所有空白字符\n\t\r空格
//   \S 匹配所有非空白字符
//   [] 指定范围的原子
    
    
    $zz = &#39;/a/&#39;;
    $string = &#39;fsadfgsadfasdfsd&#39;;
    if (preg_match($zz, $string,$matches)) {
        echo &#39;匹配到了,结果为:&#39;;
        var_dump($matches);
    }else{
        echo &#39;没有匹配到!&#39;;
    }
    
//     原子等价式
//     \w [a-zA-Z0-9_]
//     \W [^a-zA-Z0-9_]
//     \d [0-9]
//     \D [^0-9]
//     \s [\t\n\f\r]
//     \S [^\t\n\f\r]
    $zzz = &#39;/[^0-9A-Za-z_]/&#39;;
    $string = &#39;aaaaab311dd&#39;;
    $string1 = &#39;##@$#$%$%^^&#39;;
    if (preg_match($zzz, $string1,$matches)) {
        echo &#39;匹配成功,结果为:&#39;;
        var_dump($matches);
    }else{
        echo &#39;匹配失败!&#39;;
    }
    
    //元字符(重点)
    
    //模式匹配符
    $pattern = &#39;/^a\d+/m&#39;;
    $string3 = "学好成a9为大神,干番大事业";
    if (preg_match($pattern, $string3,$matches)) {
        echo &#39;匹配完成,结果为:&#39;;
        var_dump($matches);
    }else {
        echo &#39;匹配失败!&#39;.&#39;<br />&#39;;
    }
    
    $pattern = &#39;/新的未来.+\d+/s&#39;;
    $string = "新的未来987654321";
    if (preg_match($pattern, $string,$matches)) {
        echo &#39;匹配完成,结果为:&#39;;
        var_dump($matches);
    }else {
        echo &#39;匹配失败!&#39;;
    }
    
    $pattern = &#39;/(\w+)@(\w+).(com|org)/&#39;;
    $str = "bob@example.com";
    preg_match($pattern, $str, $match);
    print_r($match);
    
//     UBB文本编辑器
    $string4=&#39;[b]为你写诗句[/b]
    [i]为你做不可能的事[/i]
    [u]诶呀,哥不是写情诗[/u]
    [color=Red]哥是再说情歌[/color]
    [size=7]吴克群[/size]
    [qq]123123123[/qq]&#39;;
    //匹配UBB字符
    $pattern=array(
        &#39;/\[b\](.*)\[\/b\]i&#39;,
        &#39;/\[i\](.*)\[\/i\]iU&#39;,
        &#39;/\[u\](.*?)\[\/u\]/i&#39;,
        &#39;/\[color=(.*?)\](.*?)\[\/color\]/&#39;,
        &#39;/\[size=(\d)\](.*?)\[/\size\]/&#39;,
        &#39;/\[qq\](\d{5,12})\[\/qq\]&#39;
    );
    $replace=array(
        &#39;<b>\\1</b><br />&#39;,
        &#39;<i>\\1</i><br />&#39;,
        &#39;<u>\\1</u><br />&#39;,
        &#39;<font color="\\1">\\2</font><br />&#39;,
        &#39;<font size="\\1">\\2</font><br />&#39;,
        &#39;<a href="http://wpa.qq.com/msgrd?V=1&Uin
        =\\1&Site=[Discuz!]&Menu=yes"
        target="_blank"><img src="http://wpa.qq.com
        /pa?p=1:\\1:1" border="0"></a>&#39;,
    );
    $ubb = preg_replace($pattern, $replace, $string4);
    echo $ubb;
    
?>
Copy after login

相关推荐:

php基础一


The above is the detailed content of PHP Basics 2 Regular Expressions. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!