2014-02-12 33 views
0

我想從一個字符串中刪除此代碼從字符串中刪除代碼,並獲得價值

[gallery columns="2" link="none" ids="69,50" orderby="rand"] 

。 [畫廊.....]始終是相同的,並且......發生變化。我怎樣才能做到這一點?然後我想將ids值保存在另一個字符串中。什麼是最優雅的方式?謝謝。

回答

1

下面的正則表達式將整個字符串匹配將其刪除,並且將包含在提取組的ID:

/\[gallery .*?ids="(.*?)".*?\]/ 

此代碼應工作:

$string ='test i am test lol [gallery columns="2" link="none" ids="69,50" orderby="rand"] me also!'; 
preg_match('/\[gallery .*?ids="(.*?)".*?\]/', $string, $matches); 

echo $matches[1]; // prints the ids 

$string = preg_replace('/\[gallery .*?ids="(.*?)".*?\]/', '', $string); 

echo $string; // prints the cleaned string 
+0

這是完美的!非常感謝你!我沒有找到一個很好的手冊來理解這個東西:/ \ [gallery。*?id =「(。*?)」。*?\] /。你能告訴我它是如何調用的,所以我可以搜索它嗎? –