2011-04-04 00:13:33 0 Comments PHP Boy.Lee

PHP Regular Expression Code Demo

{test your Regular Expression here} 

this is really a useful tool site when u working on regular expression.

{link} http://regexpal.com/

{preg_match_all}

>>Code One : Basic Model

$str = "BoyLee's Yii Soul GardenRSS";
preg_match_all("|href='.*?'|", $str, $arrMatch);
print_r($arrMatch);
Array
(
    [0] => Array
        (
            [0] => href='http://www.yiiblog.info'
            [1] => href='http://www.yiiblog.info/blog/rss/'
        )

)

 

>>Code Two : add "()" into it

preg_match_all("|href='(.*?)'|", $str, $arrMatch);
print_r($arrMatch);
Array
(
    [0] => Array
        (
            [0] => href='http://www.yiiblog.info'
            [1] => href='http://www.yiiblog.info/blog/rss/'
        )

    [1] => Array
        (
            [0] => http://www.yiiblog.info
            [1] => http://www.yiiblog.info/blog/rss/
        )

)

// a () make the Array with label 1 born.

>>Code Three : add flag PREG_PATTERN_ORDER

preg_match_all("|href='(.*?)'|", $str, $arrMatch,PREG_PATTERN_ORDER);
print_r($arrMatch);
Array
(
    [0] => Array
        (
            [0] => href='http://www.yiiblog.info'
            [1] => href='http://www.yiiblog.info/blog/rss/'
        )

    [1] => Array
        (
            [0] => http://www.yiiblog.info
            [1] => http://www.yiiblog.info/blog/rss/
        )

)

//nothing change because this is a default flag

>>Code Four : Add Flag PREG_SET_ORDER

preg_match_all("|href='(.*?)'|", $str, $arrMatch,PREG_SET_ORDER);
print_r($arrMatch);
Array
(
    [0] => Array
        (
            [0] => href='http://www.yiiblog.info'
            [1] => http://www.yiiblog.info
        )

    [1] => Array
        (
            [0] => href='http://www.yiiblog.info/blog/rss/'
            [1] => http://www.yiiblog.info/blog/rss/
        )

)

 

{preg_match}

preg_match("|href='(.*?)'|", $str, $arrMatch);
print_r($arrMatch);
Array
(
    [0] => href='http://www.yiiblog.info'
    [1] => http://www.yiiblog.info
)

{preg_repeat}

$strNew = preg_replace("|'>|", "' target='_blank'>", $str);
echo htmlentities($strNew);
BoyLee's Yii Soul GardenRSS

//link will open in a new window now.

 

{Boy Say}

Regular Expression is very important and easy to use, so I hope this Code Demo will help u too get it immediately.Anyway Have a nice all of u ^&^.