获取大字符串中的特定字符串内容

时间:2015-06-17 16:12:53

标签: php regex string

我有一个像这样的大字符串:

[/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="作为可能子字符串的开头,将下一个字符"作为我的部分的结尾,但不知道如何执行此操作。

对于未来的价值当然,还有一种优雅的方式可以做到这一点吗?

3 个答案:

答案 0 :(得分:1)

您可以在PHP中使用此正则表达式:

post_categories="\K[^"]+

RegEx Demo

答案 1 :(得分:0)

这应该提取你想要的东西。

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

答案 2 :(得分:0)

您可以使用此正则表达式:

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

?<=(lookbehind)在所需匹配之前查找post_categories="(?=)(lookahead)在所需匹配后查找"

[^"]获取匹配项(假设不包含任何"

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