2015-06-17 46 views
2

內特定字符串的內容我有一個大的字符串是這樣的:獲得大串

[/az_column_text][/vc_column_inner][vc_column_inner width="3/4"] 
    [az_latest_posts post_layout="listed-layout" post_columns_count="2clm" post_categories="assemblea-soci-2015"] 
    [/vc_column_inner][/vc_row_inner][/vc_column] 

我需要提取什麼:

assemblea-soci-2015 

當然這個值可以改變,也是大串也可以改變。我需要一個正則表達式或其他東西來從這個大字符串中提取這個值(它總是從post_categories="my-value-to-extract")。

我認爲採取post_categories="作爲一個可能的子字符串的開始和下一個字符"作爲我的部分結束,但不知道如何做到這一點。

有沒有一種優雅的方式來做到這一點也爲未來的價值,當然,不同的長度?

+0

只需更換'如一個可能的子串和nex的開始這個'post_categories ='中的t char'作爲一個可能的子字符串的開始,下一個字符「'用'(。*?)' – sln

回答

0

你可以使用這個表達式:

(?<=post_categories=")[^"]+(?=") 

?<=(回顧後)查找post_categories="所期望的比賽之前,以及所需的賽後(?=)(前瞻)查找"

[^"]得到匹配(其被假定不包含任何"

Demo

例PHP代碼:

$text='[/az_column_text][/vc_column_inner][vc_column_inner width="3/4"] 
    [az_latest_posts post_layout="listed-layout" post_columns_count="2clm" post_categories="assemblea-soci-2015"] 
    [/vc_column_inner][/vc_row_inner][/vc_column]'; 
preg_match ("/(?<=post_categories=\")[^\"]+(?=\")/", $text,$matches); 
echo $matches[0]; 

輸出:

assemblea-soci-2015 
0

這應該提取你想要什麼。

preg_match ("/post_categories=\"(.*)\"\[\]/", $text_you_want_to_use) 
+0

這將返回空輸出 –