Regular Express IN PHP

function format_time($t){
	$pattern = '/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/';
	preg_match($pattern, $t, $matchs);
	//print_r($matchs);
	return $matchs[1] . '/' . $matchs[2] . '/' . $matchs[3] . ' ' . $matchs[4] . ':' . $matchs[5] . ':' . $matchs[6];
}

$t: 20130222000000

output: 2013/02/22 00:00:00

Check results:

if($ip_addr != '127.0.0.1'){
	$pattern = '/^150\.70\.\d{1,3}\.\d{1,3}$/';//Trend Micro IP:150.70.97.125
	preg_match($pattern, $ip_addr, $matchs);
	//echo var_dump($matchs == false);
	//exit();
	if($matchs != false){
		saveLog();
	}
}

 

function format_url($s){
	$pattern = '/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w][email protected])?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w][email protected])[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[.\!\/\\w]*))?)/';
	$replace = '<a href="${0}" target="_blank">${0}</a>';
	//preg_match($pattern, $s, $matchs);
	//print_r($matchs);
	//exit();
	return preg_replace($pattern, $replace, $s);
}

This function will format all URLs in the string.

Greedy and Lazy match:

By default the searching is greedy, but it’s not easy to find all URLs with the same pattern. Append “?” to a quantifier force it lazy search.

//Greedy
$pattern = '/<a\shref=\".*pageId\=(\d+)\"/';

//Lazy (note the ?)
$pattern = '/<a\shref=\".*?pageId\=(\d+)\"/';

Example HTML:

<p>Scenario 1 (Android, BDM)</p><p><br/></p><h1 class="with-breadcrumbs" id="Scenario1(Android,BDM)-Scenario2(Android,BDM)"><a href="https://abhelp.acer.com/pages/viewpage.action?pageId=11547494" rel="nofollow">Scenario 2 (Android, BDM)</a></h1><p><br/></p><h2 id="Scenario1(Android,BDM)-Relatedarticles">Related articles</h2><p></p><p>\n\n\n\n\n\n\n\n\n\n\n\n<ul class="content-by-label conf-macro output-block" data-hasbody="false" data-macro-name="contentbylabel">\n        <li>\n        <div>\n                <span class="icon aui-icon content-type-page" title="Page">Page:</span>        </div>\n\n        <div class="details">\n                        <a href="/pages/viewpage.action?pageId=11547147">Scenario 1 (Android, pm4)</a>\n                \n                        \n                    </div>\n    </li>\n    </ul>\n</p><p><br/></p><p><br/></p>

Get all matched results

$pattern = '/<a\shref=\".*?pageId\=(\d+)\"/';
preg_match_all($pattern, $data->content, $result);

/*
Array
(
    [0] => Array
        (
            [0] => <a href="https://abhelp.acer.com/pages/viewpage.action?pageId=11547494"
            [1] => <a href="/pages/viewpage.action?pageId=11547147"
        )

    [1] => Array
        (
            [0] => 11547494
            [1] => 11547147
        )

)

Not found result:
Array
(
    [0] => Array
        (
        )

    [1] => Array
        (
        )

)
*/

Reference:

To Exclude Specific File Extensions

/^[^.]+$|\.(?!(js|exe)$)([^.]+$)/

4 thoughts on “Regular Express IN PHP

  1. The core of your writing whilst appearing agreeable initially, did not work well with me after some time. Somewhere within the paragraphs you managed to make me a believer unfortunately just for a while. I however have a problem with your jumps in assumptions and one might do well to fill in those gaps. In the event you can accomplish that, I will definitely end up being impressed.

  2. whoah this weblog is excellent i really like reading your articles. Keep up the good work! You understand, many persons are searching around for this information, you can aid them greatly.

Comments are closed.