这是我的示例文本:
text text text
[[{"fid":"3228","view_mode":"full","fields":{"format":"full","field_file_image_alt_text[und][0][value]":"text text text","field_file_image_title_text[und][0][value]":"","field_file_image_gallery_content[und][0][value]":"","field_file_image_gallery_content[und][0][format]":"full_html"},"type":"media","link_text":null,"attributes":{"alt":"text text","height":"647","width":"421","class":"media-element file-full"}}]]
我想提取两个括号之间的文本:[[
和]]
。
这是我的方法:
preg_match_all("/\[[^\]]*\]/", $txt, $matches);
但它只适用于像[
和]
这样的单括号之间的文本。
我的正则表达式应该是什么样子的,才能提取出双括号内的文本?
发布于 2016-01-24 23:04:01
试试这个--第一个匹配:
if (preg_match('/\[\[(.*?)\]\]/i', $buffer, $regs)) {
$result = $regs[1]; // Matched text
} else {
$result = "";
}
或者在所有匹配项上使用this - iteration:
preg_match_all('/\[\[(.*?)\]\]/i', $buffer, $regs, PREG_PATTERN_ORDER);
for ($i = 0; $i < count($regs[1]); $i++) {
// Matched text = $regs[1][$i];
}
如果您没有实际的理由来处理这样的数据,请遵循Borodin的答案,并使用json_decode。这可能会更好。
发布于 2016-01-24 23:01:16
您正在尝试使用正则表达式处理JSON数据。这充满了危险。
https://stackoverflow.com/questions/34981306
复制相似问题