Regex中文匹配問題

來源:互聯網
上載者:User
如何在下面的運算式中匹配出“樣本頁面“並讀出結果?現在讀出是亂碼。

 


回複討論(解決方案)

什麼意思?匹配出樣本頁面就直接寫就行了啊。
$pattern='/樣本頁面/';

$pattern='/[樣本頁面42424242432def42242342樣本頁面dsadfa]/ u';

什麼意思?匹配出樣本頁面就直接寫就行了啊。
$pattern='/樣本頁面/';



因為在項目中“樣本頁面”這樣的字串存在於其他中文字元內。

$pattern='/[樣本頁面42424242432def42242342樣本頁面dsadfa]/ u';



謝謝版主回複 ,可是加了u只匹配出了“示”字。

對呀!你就是這麼做的
1、方括弧的是字元列表,比配的就是單個字元
加 u 修飾是為了把 utf-8 漢字當作字元看待
2、preg_match 是只取第一組匹配的結果,preg_match_all 才是取所有匹配的結果

對呀!你就是這麼做的
1、方括弧的是字元列表,比配的就是單個字元
加 u 修飾是為了把 utf-8 漢字當作字元看待
2、preg_match 是只取第一組匹配的結果,preg_match_all 才是取所有匹配的結果


那要怎麼才能匹配出“樣本頁面”?

對呀!你就是這麼做的
1、方括弧的是字元列表,比配的就是單個字元
加 u 修飾是為了把 utf-8 漢字當作字元看待
2、preg_match 是只取第一組匹配的結果,preg_match_all 才是取所有匹配的結果



這是 preg match all 的結果,還是不行呀

array(1) { [0]=> array(21) { [0]=> string(3) "示" [1]=> string(3) "例" [2]=> string(3) "頁" [3]=> string(3) "面" [4]=> string(1) "s" [5]=> string(1) "a" [6]=> string(1) "f" [7]=> string(1) "d" [8]=> string(1) "a" [9]=> string(1) "s" [10]=> string(1) "f" [11]=> string(1) "d" [12]=> string(1) "a" [13]=> string(1) "s" [14]=> string(1) "f" [15]=> string(1) "d" [16]=> string(1) "s" [17]=> string(1) "a" [18]=> string(1) "f" [19]=> string(1) "a" [20]=> string(1) "f" } }

如果你要匹配到片語 樣本頁面
那就簡單的 $pattern='/樣本頁面/';

如果你要匹配到片語 樣本頁面
那就簡單的 $pattern='/樣本頁面/';


問題是,實際項目中這個‘樣本頁面’不是單獨出現的,他是一個對象數組的結果。就是把好多類似‘樣本頁面’(‘樣本頁面1’、‘樣本頁面2’等等)這樣的字串放到了一起,然後拿去和$subject="世界,您好!樣本頁面safdasfdasfdsafaf0808080勢";匹配,看$subject是否包含‘樣本頁面’、‘樣本頁面1’、‘樣本頁面2’等中的任何一個,如果返回TRUE,則把匹配到的結果取出來備做他用。

那有什嗎?
你要的不就是這樣嗎

$subject = "世界,您好!樣本頁面safdasfdasfdsafaf0808080勢";$pattern = '/樣本頁面/';if(preg_match($pattern, $subject)) echo '包含';

或是

$subject = "世界,您好!樣本頁面safdasfdasfdsafaf0808080勢";$pattern = '/樣本頁面.+/';if(preg_match($pattern, $subject, $matches)) {  echo ,$matches[0];}

或是

$subject = "世界,您好!樣本頁面safdasfdasfdsafaf0808080勢";$pattern = '/樣本頁面.+/';if(preg_match($pattern, $subject, $matches)) {  echo ,$matches[0];}



不是,就是:

$subject="世界,您好!樣本頁面safdasfdasfdsafaf0808080勢";$pattern='/[樣本頁面42424242432def42242342樣本頁面dsadfa]/';preg_match($pattern,$subject,$matches);if (preg_match){echo '成功';}

或者這麼看吧:

$subject="世界,您好!樣本頁面safdasfdasfdsafaf0808080勢";$pattern='/[樣本頁面424世界,您好24242432de勢f42242342樣本頁面dsadfa]/';preg_match($pattern,$subject,$matches);if (preg_match){echo '成功'.'$matches[0]'.'$matches[1]','$matches[2]'}//$matches[0]應該輸出樣本頁面,$matches[1]應該輸出世界,您好,$matches[2]輸出勢

那是不可能的!

那是不可能的!


哦,那就是說我要想辦法把/[樣本頁面424世界,您好24242432de勢f42242342樣本頁面dsadfa]/分隔開然後一個個拿去對了?

那是不可能的!



正則用不了,那下面的情況怎麼實現,求版主給個思路;

假設有個數組$A,裡面放了38組資料,如何一次性隨機取出38個值 ?
如下代碼所示:

            //首先調出資料庫中的title            $linktitle=$wpdb->get_results("SELECT post_title FROM $wpdb->posts WHERE post_status = 'publish'");            $nums=rand(1,38);            $post_title=$linktitle[$nums]->post_title;//裡面有38個值            if ($post_title){                        //如果這38個值裡有任意一個在文章中出現,則執行以下函數操作            $replace = array(                   $post_title => ''.$post_title.'',               );             $text = str_replace(array_keys($replace), $replace, $text);}                                      return $text; 

你是要做關鍵次匹配?

你是要做關鍵次匹配?



大概是這樣的意思,在文章中找到所有包含在$linktitle中的關鍵詞,然後再運行替換。

$pattern = "/世界,您好|樣本頁面|勢/";
拼接這樣的字串,應該不是難事吧?

$pattern = "/世界,您好|樣本頁面|勢/";
拼接這樣的字串,應該不是難事吧?



少了可以這樣人工拼接,但實際情況會有上萬個。。。。

少時,也不是人工拼接,而是用 join 串連數組元素為字串
多時,就要改還思路:不是檢查關鍵詞是否包含在內容中,而是檢查內容中含有那些關鍵詞
這個用 trie 樹就輕鬆搞定(精華區中有)

少時,也不是人工拼接,而是用 join 串連數組元素為字串
多時,就要改還思路:不是檢查關鍵詞是否包含在內容中,而是檢查內容中含有那些關鍵詞
這個用 trie 樹就輕鬆搞定(精華區中有)



成功了,不過沒用你說的那個,是這樣寫的:

$linktitle=$wpdb->get_results("SELECT post_title FROM $wpdb->posts WHERE post_status = 'publish'");            // //遍曆KEY             $i=0;             while($i<38){                            $pattern[$i]='/'.$linktitle[$i]->post_title.'/';              $replace[$i]='this is replace result';              $i++;              }            $content=preg_replace($pattern, $replace, $content)            return $content;

我這個也是Regex,是不是一樣的啊,代碼太多就不打了。http://www.manonggu.com/biancheng/391

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.