Preg_match_all

2013-07-30   来源:站长日记       编辑:沧海桑田   类别:PHP 教程    转载到:    发表评论

preg_match_all 全局正则表达式匹配

preg_match_all 全局正则表达式匹配

preg_match_all 正规表达式中的/..../uis u i s 是有什么用
/u 表示按unicode(utf-8)匹配(主要针对多字节比如汉字)
/i 表示不区分大小写(如果表达式里面有 a, 那么 A 也是匹配对象)
/s 表示将字符串视为单行来匹配

$data ='

DDD
dd
ee
ff
ggg';
preg_match_all("/]*?>(.|n)*?", $data, $matches);
print_r($matches);

# additionally, wrap the text an $p_wrap character intervals if the config is set
function string_nl2br( $p_string, $p_wrap = 100 ) {
    $output = '';
    $pieces = preg_split( '/(]*>.*?)/is', $p_string, -1, PREG_SPLIT_DELIM_CAPTURE );
    if( isset( $pieces[1] ) ) {
        foreach( $pieces as $piece ) {
            if( preg_match( '/(]*>.*?)/is', $piece ) ) {
                $piece = preg_replace( "/]*?>/", "", $piece );
                # @@@ thraxisp - this may want to be replaced by html_entity_decode (or equivalent)               
                # if other encoded characters are a problem
                $piece = preg_replace( "/ /", " ", $piece );
                if( ON == config_get( 'wrap_in_preformatted_text' ) ) {
                    $output .= preg_replace( '/([^n]{' . $p_wrap . '})(?!)/', "$1n", $piece );
                } else {
                    $output .= $piece;
                }
            } else {
                $output .= nl2br( $piece );
            }
        }
        return $output;
    } else {
        return nl2br( $p_string );
    }
}

12

2
12|2 | 鲜花 VS 砸蛋 | 45阅读 0评论