<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>邪罗刹的菠萝阁 &#187; regex</title>
	<atom:link href="http://www.rainmoe.com/tag/regex/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.rainmoe.com</link>
	<description>One code, one world ...</description>
	<lastBuildDate>Thu, 29 Dec 2011 14:04:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>抓取并下载CSS中所有图片文件</title>
		<link>http://www.rainmoe.com/2010/01/17/catch-images-from-css/</link>
		<comments>http://www.rainmoe.com/2010/01/17/catch-images-from-css/#comments</comments>
		<pubDate>Sun, 17 Jan 2010 13:55:34 +0000</pubDate>
		<dc:creator>小邪</dc:creator>
				<category><![CDATA[作品 [Work]]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[regex]]></category>

		<guid isPermaLink="false">http://www.evlos.org/?p=1900</guid>
		<description><![CDATA[> 嘎嘎，小邪最近没啥新点子，强力寻找灵感中，所以只好给 PHP 随便找点儿事情做鸟 ╮(╯▽╰)╭。

> 今天就让 PHP 用正则式把 CSS 文件中的所有图片文件，都从 CSS 原来的位置下载来吧。



<span class="readmore"><a href="http://www.rainmoe.com/2010/01/17/catch-images-from-css/" title="抓取并下载CSS中所有图片文件">阅读全文——共2843字</a></span>]]></description>
			<content:encoded><![CDATA[<p>> 嘎嘎，小邪最近没啥新点子，强力寻找灵感中，所以只好给 PHP 随便找点儿事情做鸟 ╮(╯▽╰)╭。<br />
> 今天就让 PHP 用正则式把 CSS 文件中的所有图片文件，都从 CSS 原来的位置下载来吧。</p>
<p><img src='http://www.rainmoe.com/wp-content/uploads/old/Capture1151.jpg' /></p>
<p>> 这篇文章的亮点是，正则式更加复杂鸟，╮(-_-)╭，再就是 Copy 函数的灰常强大的一个用法。<br />
> 话说刚才听 NsYta 说小邪的主题太白了，杯具。最近太忙，没有空，不然就自己搞一个新主题。</p>
<p><span id="more-1900"></span><strong>一. 抓取 CSS 中的图片：</strong></p>
<p>> <strong>1. 首先做好准备工作：</strong></p>
<p>> 第一步，先把 CSS 原本的路径存到 $url 变量里，然后把 CSS 的内容保存在 abc.css 中。<br />
> 因为考虑到经常碰到多个 CSS 文件的状况，所以小邪没有直接填一个 CSS 路径。<br />
> 而是把几个 CSS 文件的内容合并到一起，全部塞到 abc.css 文件里面即可，嘎嘎嘎。</p>
<pre class="brush: php; auto-links: false; html-script: false; title: ; notranslate">
$data = file_get_contents('abc.css');
</pre>
<p>> 接着读取 CSS 文件的内容到 $data 变量中，然后用正则式把域名给取出来。<br />
> 因为这里考虑到很多图片文件用到了相对根路径，比方说 /img/1.gif 和 img/1.gif。<br />
> 然后 CSS 原地址在 http://www.evlos.org/css/ 那么上面的两个文件位置是不同的。</p>
<p>> 第一个文件在 http://www.evlos.org/img/1.gif，因为它的路径用到了相对根路径。<br />
> 而第二个在 http://www.evlos.org/css/img/1.gif，它的路径只是普通的相对路径。</p>
<pre class="brush: php; auto-links: false; html-script: false; title: ; notranslate">
$url = 'http://www.zcool.com.cn/css/';
preg_match('/(.*\/\/.*?)\//',$url,$host);
//这里用正则式把 http://www.zcool.com.cn/ 给取出来，后端不要忘记加斜杠喔。
//.*? 是懒惰匹配，也就是能匹配得越少就匹配越少的内容，这样就不会取过头了。
$host = $host[1];
</pre>
<p><img src='http://www.rainmoe.com/wp-content/uploads/old/Capture1152.jpg' /></p>
<p>> <strong>2. 把图片存储文件夹建好：</strong></p>
<p>> 小邪这里用了 is_dir 来确定文件夹是否存在，存在的话，就不用再建立第二遍了。<br />
> 呵呵，顺便说下，is_file 函数可以确定此文件是否为正常文件，也可以确定是否存在。<br />
> 但 file_exists() 优越一点，因为某次看到有人在 <a target='_blank' rel='nofollow' href='http://www.webmasterworld.com/'>Webmasterworld.com</a> 上面讨论过。</p>
<pre class="brush: php; auto-links: false; html-script: false; title: ; notranslate">
if (!is_dir('img')) { mkdir('img'); }
</pre>
<p>> <strong>3. 用正则式把图片相对地址取出来：</strong></p>
<pre class="brush: php; auto-links: false; html-script: false; title: ; notranslate">
$regex = '/url\(\'{0,1}\&quot;{0,1}(.*?)\'{0,1}\&quot;{0,1}\)/';
//这里用正则式匹配出图片地址，要考虑三种情况，即 url(1.gif) url('1.gif') url(&quot;1.gif&quot;)。
//这三种写法都是可以使用的，所以咱们就用上面的正则把里面的 1.gif 取出来。
//\'{0,1} 表示单引号可能出现1次或0次，\&quot; 则表示双引号可能出现1次或0次。
//中间必须使用懒惰匹配，不然取出来的就是 1.gif&quot; 而不是 1.gif 鸟，O(∩_∩)P。
preg_match_all($regex,$data,$result);
</pre>
<p>> <strong>4. 处理这些图片：</strong></p>
<p>> 首先使用一个循环，把上面是用正则提取出来的第一分支内容数组给处理一下。<br />
> 额，这里的第一分支表示正则式里面的第一个括号来着，呵呵，以此类推。</p>
<pre class="brush: php; auto-links: false; html-script: false; title: ; notranslate">
foreach ($result[1] as $val) {  }
</pre>
<p>> 然后是用正则式判定，因为还要考虑到这样 http://www.evlos.org/img/1.gif。<br />
> 这样是使用了完整的路径了，而不是想其他的一样是 /img/1.gif 或者 img/1.gif。<br />
> 所以单独判断一下，然后接着判断这两个，看看是 /img/1.gif 还是 img/1.gif。</p>
<pre class="brush: php; auto-links: false; html-script: false; title: ; notranslate">
if (preg_match('/^http.*/',$val)) { $target = $val; }
	else if (preg_match('/^\/.*/',$val)) { $target=$host.$val; }
		else { $target=$url.$val; }
echo $target.&quot;&lt;br/&gt;\r\n&quot;;
</pre>
<p>> 最后把文件名取出来，即 /img/1.gif 中的 1.gif，用于保存文件。</p>
<pre class="brush: php; auto-links: false; html-script: false; title: ; notranslate">
preg_match('/.*\/(.*\.\D+)$/',$val,$name);
</pre>
<p>> 然后咱们就可以开始下载了，这里要介绍一个强大的 Copy 函数用法。</p>
<pre class="brush: php; auto-links: false; html-script: false; title: ; notranslate">
if (!is_file('./img/'.$name[1])) {
	$imgc = file_get_contents($target);
	$handle = fopen('./img/'.$name[1],'w+');
	fwrite($handle,$imgc);
	fclose($handle);
}
</pre>
<p>> 上面那个是咱们的老方法了，嘎嘎，很麻烦。某次，小邪突然发现 Copy 的强大。<br />
> Copy 居然也可以下载，所以可以轻松使用下面的代码来处理，上面的可以退休鸟。</p>
<pre class="brush: php; auto-links: false; html-script: false; title: ; notranslate">
if (!is_file('./img/'.$name[1])) {
	copy($target,'./img/'.$name[1]);
}
</pre>
<p>> <strong>5. 完整源代码：</strong></p>
<p>> 使用的时候把 $url 填好即可，然后把所有 CSS 内容存到 abc.css 中即可。</p>
<pre class="brush: php; auto-links: false; html-script: false; title: ; notranslate">
&lt;?php
$url = 'http://www.zcool.com.cn/css/';
$data = file_get_contents('abc.css');
preg_match('/(.*\/\/.*?)\//',$url,$host);
$host = $host[1];
if (!is_dir('img')) { mkdir('img'); }
$regex = '/url\(\'{0,1}\&quot;{0,1}(.*?)\'{0,1}\&quot;{0,1}\)/';
preg_match_all($regex,$data,$result);
foreach ($result[1] as $val) {
	if (preg_match('/^http.*/',$val)) { $target = $val; }
		else if (preg_match('/^\/.*/',$val)) { $target=$host.$val; }
			else { $target=$url.$val; }
	echo $target.&quot;&lt;br/&gt;\r\n&quot;;
	preg_match('/.*\/(.*\.\D+)$/',$val,$name);
	if (!is_file('./img/'.$name[1])) {
		copy($target,'./img/'.$name[1]);
	}
}?&gt;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.rainmoe.com/2010/01/17/catch-images-from-css/feed/</wfw:commentRss>
		<slash:comments>112</slash:comments>
		</item>
		<item>
		<title>一个杯具和一个洗具与最近学习手记</title>
		<link>http://www.rainmoe.com/2010/01/07/a-cuptool-and-a-washtool/</link>
		<comments>http://www.rainmoe.com/2010/01/07/a-cuptool-and-a-washtool/#comments</comments>
		<pubDate>Thu, 07 Jan 2010 08:57:23 +0000</pubDate>
		<dc:creator>小邪</dc:creator>
				<category><![CDATA[奇客 [Geek]]]></category>
		<category><![CDATA[cuptool]]></category>
		<category><![CDATA[regex]]></category>

		<guid isPermaLink="false">http://www.evlos.org/?p=1891</guid>
		<description><![CDATA[> 每天一大早，小邪就想，淫荡的一天开始了 ╮(╯▽╰)╭，果然没有超出小邪的预料，哇咔咔。

> 今天是一个淫荡的星期四。上午，小邪很开心，因为小邪的杯具送到了，很帅的杯具。



<span class="readmore"><a href="http://www.rainmoe.com/2010/01/07/a-cuptool-and-a-washtool/" title="一个杯具和一个洗具与最近学习手记">阅读全文——共2366字</a></span>]]></description>
			<content:encoded><![CDATA[<p>> 每天一大早，小邪就想，淫荡的一天开始了 ╮(╯▽╰)╭，果然没有超出小邪的预料，哇咔咔。<br />
> 今天是一个淫荡的星期四。上午，小邪很开心，因为小邪的杯具送到了，很帅的杯具。</p>
<p><img src="http://www.rainmoe.com/wp-content/uploads/old/Capture1073.jpg" /></p>
<p>> 但是下午，小邪自己变成了杯具，o(╯□╰)o。因为小邪的弟弟有危险了 ( ⊙o⊙ )！<br />
> 起因是 <a target='_blank' rel='nofollow' href='http://imn.im/'>Nox</a> 和 <a target='_blank' rel='nofollow' href='http://liuyijun.com/'>619</a> 在准备创建一个高调的神秘组织，让我们一起用力地往下面看，嘎嘎。</p>
<p><span id="more-1891"></span><strong>一. 不是一般淫荡的一：</strong></p>
<p>> （= 0 =），刚才小邪在转悠，然后在 Nox 博客上面受精了，（- -+）。<br />
> 见鬼，破输入法，不改算了，小邪都习惯了，实际上小邪想说的是受惊了。<br />
> 让咱们继续讲这个事儿，话说 Nox 和 万戈兄 想弹小邪的鸡鸡，o(>﹏<)o。</p>
<p>> （- -||），杯具啊，小邪仰天长叹：“杯具啊 ~ 天大的杯具 ~ (>_<) ~”。</p>
<p><img src="http://www.rainmoe.com/wp-content/uploads/old/Capture1077.png" /></p>
<p>> Nox 扬言，如果小邪透露了他们要建立一个同志会的内幕的话，就要折磨小邪的弟弟。<br />
> 万戈兄 说了，“我要T小邪JJ，T到他说为止，HIA~HIA~HIA”。<br />
> 天哪，小邪要怎么办呐，谁给小邪送一个最新版的 “要你命3000”，让小邪得以继续繁衍。</p>
<p>> 啊哦，貌似刚刚小邪心中为自己的弟弟在担忧，结果不小心手一抖，多打了几个字。<br />
> 大家无视他们就好了。你没看见，你没看见，你一定没看见，对吧对吧。</p>
<p>> 小邪手又开始抖了，心中充满鸟恐惧 ( ⊙o⊙ )。光明在哪儿，啊，小邪的眼前一片漆黑。<br />
> 对了，这个组织和最近的活动有关，域名也和最近的活动有关，看看谁能猜到喔 O(∩_∩)O。</p>
<p>> 原文章围观传送门 - <a target='_blank' rel='nofollow' href='http://imn.im/20100106225707.html'>http://imn.im/20100106225707.html</a></p>
<p><strong>二. 小邪的杯具：</strong></p>
<p>> 话说，上次小邪参加了河蟹娱乐主办的迎圣诞免运费盖楼送杯具活动，O(∩_∩)O。<br />
> 结果灰常幸运地得到了奖励，小邪很喜欢呢 (*^__^*)，谢谢 兽兽 和 胡戈戈。</p>
<p><img src="http://www.rainmoe.com/wp-content/uploads/old/Capture1082.jpg" /></p>
<p>> 杯子很快地送到了呢，貌似还不错的，虽然黑色有点偏青，不过也好看的。<br />
> 小邪的相机现在不在身边，呵呵，只好把这张图片贴出来：“哥喝的不是水，是寂寞”。</p>
<p><img src="http://www.rainmoe.com/wp-content/uploads/old/Capture1083.jpg" /></p>
<p><strong>三. 正则式的特殊语法：</strong></p>
<p>> 下面的内容送给 Alswl 小盆友，有兴趣的童鞋就一起看看吧。</p>
<p>> 正则式里面也提供了很多的特殊语法，这里普遍被大家称为分组语法。<br />
> 关于支持性的问题，小邪查了一些资料，据说 (?!exp) 是和PHP兼容性最好的。</p>
<pre class="brush: php; auto-links: false; html-script: false; title: ; notranslate">
捕获：
(exp) - 匹配exp,并捕获文本到自动命名的组里
(?&lt;name&gt;exp) - 匹配exp,并捕获文本到名称为name的组里，也可以写成 (?'name'exp)
(?:exp) - 匹配exp,不捕获匹配的文本，也不给此分组分配组号

零宽断言：
(?=exp) - 匹配exp前面的位置
(?&lt;=exp) - 匹配exp后面的位置
(?!exp) - 匹配后面跟的不是exp的位置
(?&lt;!exp) - 匹配前面不是exp的位置

注释：
(?#comment) - 这种类型的分组不对正则表达式的处理产生任何影响
</pre>
<p><strong>四. 正则式的零宽断言：</strong></p>
<p>> 呵呵，话说对于 “断言” 这个词语的解释是 - 这个位置应该满足一定的条件。<br />
> Alswl 问小邪了一个问题，是关于多重条件的匹配问题，如图。</p>
<p><img src="http://www.rainmoe.com/wp-content/uploads/old/Capture1081.jpg" /></p>
<p>> 我们要使用的是(?!exp)，即让正则匹配出后面跟的不是 exp 的位置。<br />
> 这里的 $test 数组提供的是一系列条件，Akswl 要小邪这样子。</p>
<p>> 1. 匹配一段长度为 13 的字符串。<br />
> 2. 字符串中有连续的 8 个数字。<br />
> 3. 字符串中其他 5 个字符是任意的。</p>
<p>> <strong>Update at 2010.01.08</strong> -<br />
> 杯具，小邪昨天犯错误了，更新一下解法，灰常感谢柳城为小邪指出错误。</p>
<pre class="brush: php; auto-links: false; html-script: false; title: ; notranslate">
$regex = '/^(?!(.*?\d){9,})(?!(.*?\D){6,}).{13}$/';
</pre>
<p>> 小邪是这个样子解答的，首先两边的斜杠是 Perl 正则式的要求。<br />
> 然后两边的 ^ 和 $ 用来表示对应的是字符串的开始和结束。<br />
> 接着 .{13}，. 表示除了换行以外的所有字符，13 规定了匹配长度。</p>
<p>> 我们这里前面用了两次零宽断言，第一次，(?!exp) 这里的 exp 是 (.*?\d){9,}。<br />
> 表示数字的个数大于等于 9，零宽断言把它反过来就是数字的个数小于 9。<br />
> 第二次 exp 是 (.*?\D){6,} 表示非数字大于等于 6 个以上。<br />
> 即表示非数字的个数大于等于 6，把它反过来就是非数字的个数小于 6。</p>
<p>> 而这里 .* 表示 ?\d 和 ?\D 的前面可能有零次或更多次的其他字符。<br />
> 而这里的 ? 表示这段连续的字符会重复零次或一次。</p>
<p>> 下面是这个测试程序的源代码，可以拿去运行看看，嘎嘎。</p>
<pre class="brush: php; auto-links: false; html-script: false; title: ; notranslate">
&lt;?php
$test[0] = 'asss13336644ss'; $test[1] = 'aas15151515ss';
$test[2] = 'aa15151515sss'; $test[3] = 'aa15151515ss1';
$test[4] = 'aa15151515ss11';$test[5] = 'aa151515151ss';
$test[6] = 'aa15151511ssss';
$regex = '/^(?!(.*?\d){9,})(?!(.*?\D){6,}).{13}$/';
foreach ($test as $val) {//
	preg_match($regex,$val,$result);
	print_r($result);
	echo &quot;&lt;br/&gt;\r\n&quot;;
}
// 运行上面的代码就会出现以下结果 -
// Array ( )
// Array ( [0] =&gt; aas15151515ss )
// Array ( [0] =&gt; aa15151515sss )
// Array ( )
// Array ( )
// Array ( )
// Array ( )
?&gt;
</pre>
<p><strong>五. 以下是小邪昨天犯的错误（Update at 2010.01.08）</strong>：</p>
<pre class="brush: php; auto-links: false; html-script: false; title: ; notranslate">
$regex = '/^(?!\d{8,})(?!\D{5,}).{13}$/';
</pre>
<p>> 小邪是这个样子解答的，首先两边的斜杠是 Perl 正则式的要求。<br />
> 然后两边的 ^ 和 $ 用来表示对应的是字符串的开始和结束。<br />
> 接着 .{13}，. 表示除了换行以外的所有字符，13 规定了匹配长度。</p>
<p>> 我们这里前面用了两次零宽断言，第一次，(?!exp) 这里的 exp 是 \d{8,}。<br />
> 表示数字的数目在 8 个以上，第二次 exp 是 \D{5,} 表示非数字有5个以上。</p>
<p>> 因为零宽断言是让正则匹配出后面跟的不是 exp 的位置。<br />
> 所以我们匹配到的是数字不在 8 个以上，数字小于等于 8 的字符串。<br />
> 同时非数字小于等于 5 的字符串，最后在右边用 13 来确定整段的长度即可。</p>
<p>> 下面是这个测试程序的源代码，可以拿去运行看看，呵呵。</p>
<pre class="brush: php; auto-links: false; html-script: false; title: ; notranslate">
&lt;?php
$test[0] = 'asss13336644ss'; $test[1] = 'aas15151515ss';
$test[2] = 'aa15151515sss'; $test[3] = 'aa15151515ss1';
$test[4] = 'aa15151515ss11';
$regex = '/^(?!\d{8,})(?!\D{5,}).{13}$/';
foreach ($test as $val) {
	preg_match($regex,$val,$result);
	print_r($result);
	echo &quot;&lt;br/&gt;\r\n&quot;;
}
// 运行上面的代码就会出现以下结果 -
// Array ( ) Array ( [0] =&gt; aas15151515ss )
// Array ( [0] =&gt; aa15151515sss ) Array ( [0] =&gt; aa15151515ss1 )
// Array ( )
?&gt;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.rainmoe.com/2010/01/07/a-cuptool-and-a-washtool/feed/</wfw:commentRss>
		<slash:comments>138</slash:comments>
		</item>
		<item>
		<title>使用正则式整理数据库中IMG标签</title>
		<link>http://www.rainmoe.com/2010/01/05/use-regex-to-clear-the-mix-of-tag-img/</link>
		<comments>http://www.rainmoe.com/2010/01/05/use-regex-to-clear-the-mix-of-tag-img/#comments</comments>
		<pubDate>Mon, 04 Jan 2010 16:40:35 +0000</pubDate>
		<dc:creator>小邪</dc:creator>
				<category><![CDATA[作品 [Work]]]></category>
		<category><![CDATA[img]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[regex]]></category>

		<guid isPermaLink="false">http://www.evlos.org/?p=1887</guid>
		<description><![CDATA[> 小邪最近因为流量紧缺启用了防盗链系统来着，所以为了保证防盗警示图片的大小正常。

> 只好把所有图片的 IMG 标签中除了 SRC 指向图片的 URL 属性以外，其他全部清除。



<span class="readmore"><a href="http://www.rainmoe.com/2010/01/05/use-regex-to-clear-the-mix-of-tag-img/" title="使用正则式整理数据库中IMG标签">阅读全文——共1129字</a></span>]]></description>
			<content:encoded><![CDATA[<p>> 小邪最近因为流量紧缺启用了防盗链系统来着，所以为了保证防盗警示图片的大小正常。<br />
> 只好把所有图片的 IMG 标签中除了 SRC 指向图片的 URL 属性以外，其他全部清除。</p>
<p><img src="http://www.rainmoe.com/wp-content/uploads/old/Capture1056.jpg" /></p>
<p>> 因为国内网上的正则式教程十分紧缺，只能找到少量的教程，有时候很难学习全面。<br />
> 所以小邪也只能按照自己多次的测试得出的结果来稍微讲解一下呢，能学多少就看你的咯。</p>
<p><span id="more-1887"></span><strong>一. 有关正则式的函数分类：</strong></p>
<p>> <strong>正则表达式函数库（Perl 兼容）</strong>-</p>
<p>> preg_grep --  返回与模式匹配的数组单元<br />
> preg_match_all -- 进行全局正则表达式匹配<br />
> preg_match -- 进行正则表达式匹配<br />
> preg_quote -- 转义正则表达式字符<br />
> preg_replace_callback -- 用回调函数执行正则表达式的搜索和替换<br />
> preg_replace -- 执行正则表达式的搜索和替换<br />
> preg_split -- 用正则表达式分割字符串</p>
<p>> <strong>正则表达式函数库（POSIX 扩展）</strong>-</p>
<p>> ereg_replace -- 替换正则表达式<br />
> ereg -- 正则表达式匹配<br />
> eregi_replace -- 不区分大小写替换正则表达式<br />
> eregi -- 不区分大小写的正则表达式匹配<br />
> split -- 用正则表达式将字符串分割到数组中<br />
> spliti --  用正则表达式不区分大小写将字符串分割到数组中<br />
> sql_regcase --  产生用于不区分大小的匹配的正则表达式</p>
<p>> 这两组函数库提供了对 POSIX 和 PERL 两种风格的正则表达式的支持。<br />
> 区别不大，PERL 的表达式两边要加上 “/”，而且据说 PERL 运行效率高一点。</p>
<p><strong>二. 程序运行LOG日志记录：</strong></p>
<p>> 呵呵，这个是程序运行的时候会陆续输出的运行记录，方便检查。<br />
> 第一次运行的时候像下面这样，然后刷新，被替换和替换为的数据会变成相同的。<br />
> 这样就表示替换成功了，恭喜发财，嘿嘿嘿 O(∩_∩)O。</p>
<pre class="brush: php; auto-links: false; html-script: false; title: ; notranslate">
Post Id =&gt; 1864
//正在被替换的数据库中文章的ID
&lt;img src=&quot;http://www.evlos.org/uploads/1451_020.jpg&quot;
 class=&quot;alignnone&quot; width=&quot;600&quot; height=&quot;437&quot; /&gt;
//在数据库中的文章表里面查找到的IMG标签
&lt;img src=&quot;http://www.evlos.org/uploads/1451_020.jpg&quot; /&gt;
//将要被替换成为的精简过的IMG标签
ID Count : 1
//此文章中的图片数量统计
Success ~!
//成功更新了Mysql数据库中的数据

Post Id =&gt; 1875
&lt;img src=&quot;http://www.evlos.org/uploads/1451_031.jpg&quot;
 class=&quot;alignnone&quot; width=&quot;600&quot; height=&quot;235&quot; /&gt;
&lt;img src=&quot;http://www.evlos.org/uploads/1451_031.jpg&quot; /&gt;
&lt;img src=&quot;http://www.evlos.org/uploads/1451_030.jpg&quot;
 class=&quot;alignnone&quot; width=&quot;600&quot; height=&quot;156&quot; /&gt;
&lt;img src=&quot;http://www.evlos.org/uploads/1451_030.jpg&quot; /&gt;
&lt;img src=&quot;http://www.evlos.org/uploads/1451_031.jpg&quot;
 class=&quot;alignnone&quot; width=&quot;600&quot; height=&quot;235&quot; /&gt;
&lt;img src=&quot;http://www.evlos.org/uploads/1451_031.jpg&quot; /&gt;
&lt;img src=&quot;http://www.evlos.org/uploads/1451_028.jpg&quot;
 class=&quot;alignnone&quot; width=&quot;600&quot; height=&quot;186&quot; /&gt;
&lt;img src=&quot;http://www.evlos.org/uploads/1451_028.jpg&quot; /&gt;
&lt;img src=&quot;http://www.evlos.org/uploads/1451_029.jpg&quot;
 class=&quot;alignnone&quot; width=&quot;600&quot; height=&quot;246&quot; /&gt;
&lt;img src=&quot;http://www.evlos.org/uploads/1451_029.jpg&quot; /&gt;
ID Count : 5
Success ~!

-------------------
Count : 296 //整个WP_posts表中，所有的的图片总数
</pre>
<p><strong>三. 程序源代码：</strong></p>
<p>>  以下内容保存为任意名称的 PHP 文件，填好配置，运行即可。</p>
<pre class="brush: php; auto-links: false; html-script: false; title: ; notranslate">
&lt;?php
$sqlc_host = &quot;localhost&quot;; //Mysql服务器地址
$sqlc_user = &quot;&quot;; //用户名
$sqlc_psw = &quot;&quot;; //密码
$sqlc_dba = &quot;wordpress&quot;; //数据库名
echo '&lt;html&gt;&lt;head&gt;
&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot; /&gt;
&lt;title&gt;Img标签批量清洗程序&lt;/title&gt;
&lt;/head&gt;&lt;body&gt;';
$img_count = 0;
$sqlc_con = mysql_connect($sqlc_host,$sqlc_user,$sqlc_psw);
mysql_select_db($sqlc_dba,$sqlc_con);
mysql_query(&quot;set names UTF8&quot;);
$sqlc_inner = mysql_query(&quot;SELECT * FROM wp_posts&quot;);
while($sqlc_row = mysql_fetch_array($sqlc_inner)) {
	$cid = $sqlc_row['ID'];
	echo &quot;&lt;br /&gt;\n&quot;.'Post Id =&gt; '.$cid.&quot;&lt;br /&gt;\n&quot;;
	$img_change = $sqlc_row['post_content'];
	$img_preg = '/&lt;img src=[^&gt;]+&gt;/';
	preg_match_all($img_preg,$img_change,$img_find);
	$img_count_id = $img_count;
	foreach ($img_find[0] as $value) {
		$img_count++;
		$value_echo = str_ireplace('&lt;','&amp;lt;',$value);
		$value_echo = str_ireplace('&gt;','&amp;gt;',$value_echo);
		echo $value_echo.&quot;&lt;br/&gt;\n&quot;;
		preg_match(&quot;(http://.+\.(jpg|png|JPG|PNG))&quot;,$value,$img_url);
		if (isset($img_url[0])) {
			$result = '&lt;img src=&quot;'.$img_url[0].'&quot; /&gt;';
		}
		else {
			$result = '';
		}
		$result_echo = str_ireplace('&lt;','&amp;lt;',$result);
		$result_echo = str_ireplace('&gt;','&amp;gt;',$result_echo);
		echo $result_echo.&quot;&lt;br/&gt;\n&quot;;
		str_ireplace(&quot;\n&quot;,&quot;&quot;,$value);
		if ($result &lt;&gt; '&lt;img src=&quot;&quot; /&gt;'&amp;&amp;$cid &lt;&gt; 1887&amp;&amp;$result &lt;&gt; '') {
			$img_change = str_ireplace($value,$result,$img_change);
		}
	}
	echo 'ID Count : '.($img_count - $img_count_id).&quot;&lt;br/&gt;\n&quot;;
	$img_change = addslashes($img_change);
	$change = $img_change;
	if (mysql_query(&quot;update wp_posts set post_content='&quot;.$change.&quot;' where id=&quot;.$cid.&quot;&quot;)) {
		echo 'Success ~!';
	}
	else {
		echo &quot;Error : &quot; . mysql_error();
	}
}
echo '&lt;br/&gt;-------------------&lt;br/&gt;Count : '.$img_count;
echo '&lt;/body&gt;&lt;/html&gt;';
mysql_close($sqlc_con);
?&gt;</pre>
<p><strong>四. 程序运行原理：</strong></p>
<pre class="brush: php; auto-links: false; html-script: false; title: ; notranslate">
mysql_query(&quot;set names UTF8&quot;);
//这里将数据库查询时候的字符集设定为 UTF-8，否则得到的数据会是乱码

$img_preg = '/&lt;img src=[^&gt;]+&gt;/';
preg_match_all($img_preg,$img_change,$img_find);
//这里使用正则式将 IMG 标签全部找出来，并把整个标签放到 $img_find 变量里

preg_match(&quot;(http://.+.(jpg|png))&quot;,$value,$img_url);
//这里把 IMG 标签之中的 URL 提取出来

$result = '&lt;img src=&quot;'.$img_url[0].'&quot; /&gt;';
if ($result &lt;&gt; '&lt;img src=&quot;&quot; /&gt;') {
	$img_change = str_ireplace($value,$result,$img_change);
}
//这里把提取之后重新组合的 IMG 标签替换进去

$img_change = addslashes($img_change);
//小邪觉得这个是亮点，这个函数能够让我们把 HTML 代码顺利存储到数据库
</pre>
<p><strong>五. 程序中使用过的正则式：</strong></p>
<pre class="brush: php; auto-links: false; html-script: false; title: ; notranslate">
$img_preg = &quot;/&lt;img src=[^&gt;]+&gt;/&quot;;
</pre>
<p>> 找到以首先找到 IMG 标签的头部，然后后面的中括号里是一个条件。<br />
> 条件表达的是，这里跟着的字符是除了右尖括号以外的字符。<br />
> 右中括号的后边是个加号，意思是 “重复一次或更多次”。<br />
> 最后以右尖括号作为结尾，这样子就可以捕捉 IMG 标签咯。</p>
<pre class="brush: php; auto-links: false; html-script: false; title: ; notranslate">
$img_url_preg = &quot;(http://.+.(jpg|png))&quot;
</pre>
<p>> 首先这里两边要用括号，表示包含分枝条件。然后找到以 HTTP:// 开头的字符。<br />
> 接着用 “.” 表示 “匹配除换行符以外的任意字符”，然后用加号标示重复次数。<br />
> 加一个斜杠标示转义字符，就是说后面那个点是普通字符，不是正则式的内容。<br />
> 然后加个括号，里面是分枝条件，标示最后是以 jpg 或者 png 结尾的。</p>
<p><strong>六. 常用的正则式语法：</strong></p>
<pre class="brush: php; auto-links: false; html-script: false; title: ; notranslate">
.	匹配除换行符以外的任意字符
w	匹配字母或数字或下划线或汉字
s	匹配任意的空白符
d	匹配数字
	匹配单词的开始或结束
^	匹配字符串的开始
$	匹配字符串的结束

*	重复零次或更多次
+	重复一次或更多次
?	重复零次或一次
{n}	重复n次
{n,}	重复n次或更多次
{n,m}	重复n到m次

W	匹配任意不是字母，数字，下划线，汉字的字符
S	匹配任意不是空白符的字符
D	匹配任意非数字的字符
B	匹配不是单词开头或结束的位置
[^x]	匹配除了x以外的任意字符
[^aeiou]	匹配除了aeiou这几个字母以外的任意字符

*?	重复任意次，但尽可能少重复
+?	重复1次或更多次，但尽可能少重复
??	重复0次或1次，但尽可能少重复
{n,m}?	重复n到m次，但尽可能少重复
{n,}?	重复n次以上，但尽可能少重复
</pre>
<p><strong>七. Addslashes 预处理函数：</strong></p>
<pre class="brush: php; auto-links: false; html-script: false; title: ; notranslate">
&lt;?php
$a = &quot;img src=&quot;'1''2'20100101/18/59_006.jpg&quot; /&quot;;
echo addslashes($a);
?&gt;
//输出 img src=\&quot;\'1\'\'2\'20100101/18/59_006.jpg\&quot; /
</pre>
<p>> 预处理之后的样子在上面代码框中，这样存储就不会出现错误了。<br />
> 而使用 Phpmyadmin 或者 mysql_query 之类的东东读取数据库的数据。<br />
> 得到的都是预处理前的数据，灰常奇妙 (*^__^*) 嘻嘻。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.rainmoe.com/2010/01/05/use-regex-to-clear-the-mix-of-tag-img/feed/</wfw:commentRss>
		<slash:comments>82</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using memcached (Feed is rejected)
Page Caching using memcached
Database Caching 1/19 queries in 0.013 seconds using memcached
Object Caching 321/365 objects using memcached

Served from: www.rainmoe.com @ 2012-02-09 16:59:34 -->
