谏知中文网

悟已往之不谏,知来者之可追

项目中正则匹配的使用

发表于 2016-06-22 2285 次浏览
  1. 伪静态后分页格式:

function url($page, $url){
$url = explode('?',$url);
$has = preg_match('/[^\-]+\.?(?=\.)/',$url[0]); //判断url格式是否为/99-88-66.html
if($has){
$url = preg_replace('/[^\-]+\.?(?=\.)/',$page,$url[0]);
return $url;
}else{
return str_replace(urlencode('[PAGE]'), $page, $this->url); //old
}
}
echo url(4, "/show-2-876-1.html");

2.content中标签内容:

$content = $str = '[build]5.24日[/build]安卓工智能识定[build]经济可达2000亿美元[/build]';
if(preg_match_all("|\[build\](.*)\[/build\]|U", $content, wholesale jerseys china $m, PREG_PATTERN_ORDER)) {
foreach($m[1] as $key => $rt) {
$str = str_replace($m[0][$key], 'hello jync', $str);
}
echo $str;
}
hello jync安卓谷歌人工智能识定hello jync

3.content中img来源截取:

$info['content'] = '<img src="/wp-content/themes/Nana/images/random/5.jpg" />';
if(preg_match("/&lt;\s*img\s+[^&gt;]*?src\s*=\s*(\'|\")(.*?)\\1[^&gt;]*?\/?\s*&gt;/i",
$info['content'], $match)) {
$data['thumb'] = str_replace('http://img.jyncode.com/', '', $match[2]);
}
echo $data['thumb'];
/wp-content/themes/Nana/images/random/5.jpg

4.mysql replace内容替换:

update news set content = 
replace(content, 'http://www.jyncode.com/uploadfile/', '');
http://www.jyncode.com/uploadfile/1463713952761882.jpg
1463713952761882.jpg

5.regexp局部匹配:

SELECT * FROM `diyhits` WHERE `url` REGEXP '^http://news.';
http://news.sohu.com
http://news.youku.com/list/87.html