使用 PCRE 尋找指定的字串區塊,並將此區塊內特定的字串替換。

從 WordPress.com ‘s Blog 把文章匯出成xml檔案,匯出後就可以拿來匯入自已架的 WordPress 網站裏。這樣匯出匯入的步驟並不難,但是如果遇到文章內容包含[sourcecode lang="sh"]….[/sourcecode], 這類的程式語法標籤時,匯入自已架的 WordPress 後顯示出來的結果可能和我們預期的不一樣。

我們檢查匯出的XML檔案,可以發現[sourcecode]的標籤區塊雖然存在,但是它裏面包含的一些特殊符號,已經被轉換成 html 的符號編碼(程式語法一定會包含需要Escapse的符號,例如“或是&、…)。

因此我們在XML檔案中看到的[sourcecode]程式碼區塊,裏面內容是長這個樣子的。

為了讓程式碼區塊匯入後,可以正常呈現它應有的樣子。須在匯入前還原區塊內被替換掉的這些特殊符號,如此匯入後就能正確呈現程式碼它應有的樣式了。

現在就寫個php的程式碼來幫我們處理這些被替換掉的符號吧!我們會用到 PCRE 正規表式法這個函式庫,利用preg_replace()及preg_replace_callback()這兩個函式來達成。

執行步驟:

  1. 找出以 [sourcecode 為開頭和以 [/sourcecode]

    為結尾的字串區塊。

  2. 在被找到的區塊中搜尋與取代這些被取代為 html escapse code 的符號,以及取代「sourcecode」標籤為「pre」。

php script: 字串取代 WordPress.com 匯出的檔案

<?php 
function sub_replace($matches){
        $patterns = array('/&gt;/','/&lt;/','/&amp;/','/&quot;/','/\/','/\[\/sourcecode\]/','/language=/');
        $replacements = array('>','<','&','"','<pre${1}>','</pre>','lang=');
        return  preg_replace($patterns,$replacements,$matches[0]);
}
$file_path="";
if($argv[1]==null){
        fwrite(STDOUT,"請輸入由「wordpress.com」匯出XML的檔案路徑:");
        $file_path=trim(chop(fgets(STDIN)));
}
else{
        $file_path=$argv[1];
}

if(!file_exists($file_path)){
        fwrite(STDOUT,"找不到檔案,請檢查檔案「{$file_path}」是否存在?\n");
        exit ;
}
echo "你輸路的檔案路徑為「{$file_path}」。\n";
$wordpress=fopen($file_path,"r");
$string=fread($wordpress,filesize($file_path));
fclose($wordpress);
$new_string = preg_replace_callback('/\/ims',"sub_replace",$string);
//echo "------------------處理後的檔案內容--------------------\n";
//echo $new_string."\n";
$new_file_path=exec("pwd")."/replaced_file.xml";
$new_file=fopen($new_file_path,"w");
fwrite($new_file,$new_string);
fclose($new_file);
echo "取代內容已輸出到「".$new_file_path."」。\n";
?>

PHP preg 正規表示的使用語法在網路上都可以查詢,還有 preg 讓貪婪符合變的不貪婪的作法是將「/.*/」換作「/.*?/」,問號就是不貪婪的關鍵符號。

執行PHP腳本讓匯出的檔案被替換掉的字串再換回來原來的樣子就可以拿來匯入自已架設的Wordpress。

下面則是已經匯入Wordpress資料庫後的PHP處理腳本。

<?php
//取代被轉換為htmlcode的字串
function sub_replace($matches){
        //echo $matches[0];
        $patterns = array('/(&lt;|lt;)/','(&gt;|gt;)','(&amp;)','(&quot;|quot;)','|\|','|\|');
        $replacements = array('<','>','&','"','<pre${1}>','</pre>');
        //做兩次取代,因為匯入資料庫後htmlcode編碼有時會少&符號。
        return preg_replace($patterns,$replacements,preg_replace($patterns,$replacements,$matches[0]));
}
//連線wordpress資料庫
$link = mysql_connect('localhost', 'root', 'xxxxxx');
if (!$link) {
        die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully'."\n";
//選擇連線資料庫
mysql_select_db("blog");
mysql_query("set names utf8");

//取得結果集將文章內容包含有“...&#091/sourcecode])
while($row = mysql_fetch_assoc($result)) {
        $update_str="";
        $content = $row['post_content'];
        //字串取代並處理特殊字符供插入資料庫用
        $content =  mysql_real_escape_string(preg_replace_callback("|\|Usm",'sub_replace',$content));
        $update_str="update wp_posts set post_content='".$content."' where ID='".$row['ID']."';";
        mysql_query($update_str);
        //      break;
}
mysql_free_result($result);
mysql_close($link);
<?>

發表留言