Skip to content

Instantly share code, notes, and snippets.

@millken
Created October 29, 2010 09:10
Show Gist options
  • Select an option

  • Save millken/653188 to your computer and use it in GitHub Desktop.

Select an option

Save millken/653188 to your computer and use it in GitHub Desktop.
小技巧
editplus正则查找php函数
^[ \t]*(public |private |protected |static )[ \t]*(static[ \t]*)?function[ \t].*\([^;]*$
preg_match_all("~href=\"([^\"]+\.css)(?:.*?)\"[^>]*>~isx",$html,$links); 捕获html的link style链接
function get_all_url($code){
preg_match_all('/<as+href=["|']?([^>"' ]+)["|']?s*[^>]*>([^>]+)</a>/i',$code,$arr);
return array('name'=>$arr[2],'url'=>$arr[1]);
}
/*======================================================================*\
Function: _expandlinks
Purpose: expand each link into a fully qualified URL
Input: $links the links to qualify
$URI the full URI to get the base from
Output: $expandedLinks the expanded links
\*======================================================================*/
function _expandlinks($links,$URI)
{
preg_match("/^[^\?]+/",$URI,$match);
$match = preg_replace("|/[^\/\.]+\.[^\/\.]+$|","",$match[0]);
$match = preg_replace("|/$|","",$match);
$match_part = parse_url($match);
$match_root =
$match_part["scheme"]."://".$match_part["host"];
$search = array( "|^http://".preg_quote($this->host)."|i",
"|^(\/)|i",
"|^(?!http://)(?!mailto:)|i",
"|/\./|",
"|/[^\/]+/\.\./|"
);
$replace = array( "",
$match_root."/",
$match."/",
"/",
"/"
);
$expandedLinks = preg_replace($search,$replace,$links);
return $expandedLinks;
}
@millken

millken commented Jan 12, 2011

Copy link
Copy Markdown
Author

preg_match_all("~href="([^\"]+.css)(?:.?)"[^>]>~isx",$html,$links); 捕获html的link style链接

@millken

millken commented Jan 13, 2011

Copy link
Copy Markdown
Author

preg_match_all("/url(\s_(?:["'])?(._?)\s*(?:["'])?)/isx", $c, $imagesURLArray); ?:表示不存储 ?表示0,1
preg_match_all("/src=(?:["'])([^'\"]+)/i", $cssContent, $imagesURLArray);

匹配CSS里的图片

@millken

millken commented Jan 13, 2011

Copy link
Copy Markdown
Author

$c = file_get_contents('meituan.css');
preg_match_all("/url(\s_(?:["'])?(._?)\s*(?:["'])?)|src=(?:["'])([^'\"]+)/isx", $c, $imagesURLArray);
$imagesURLArray = array_unique(array_filter(array_merge((array)$imagesURLArray[1], $imagesURLArray[2])));
usort($imagesURLArray, "cmp");
function cmp($a, $b) {
if (strlen($a) == strlen($b)) {
return 0;
}
return (strlen($a) > strlen($b)) ? -1 : 1;
}
d($imagesURLArray);

@millken

millken commented Jan 13, 2011

Copy link
Copy Markdown
Author

PHP完美的提取链接正则

今天把Snoopy里的提取链接函数挖出来,并加强了一下
function match_links($document) {
preg_match_all("'<\s_a\s.?href\s=\s_(["'])?(?(1)(.?)\1|([^\s>]+))[^>]>?(.*?)'isx",$document,$links);
while(list($key,$val) = each($links[2])) {
if(!empty($val))
$match['link'][] = $val;
}
while(list($key,$val) = each($links[3])) {
if(!empty($val))
$match['link'][] = $val;
}
while(list($key,$val) = each($links[4])) {
if(!empty($val))
$match['content'][] = $val;
}
while(list($key,$val) = each($links[0])) {
if(!empty($val))
$match['all'][] = $val;
}
return $match;
}

@millken

millken commented Mar 9, 2011

Copy link
Copy Markdown
Author

editplus 正则查找所有注释行

^[\t ]*[//|\/\*|*].*\n

komodo正则替换smarty变量
<{($.*)}>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment