使用 preg_replace 删除引号之间的内容以外的所有内容
Using preg_replace to remove everything except content between quotes
我有以下简码,我正在尝试 运行 a preg_replace,以获取标题值。
[tab title="Emergency Contact" important="true"]
目前,我使用以下过滤器:
$sanitized_title = $tab_title ? preg_replace("/[\”\"\’\']*|(?:’|”)*\]/i", "", preg_replace("/\[tab *title=[\”\"\’\']*|(?:’|”)*/i", "", $tab_title[0])) : __("Open Tab");
这个returns"Emergency Contact important=true",这不是我需要的。我基本上是想得到类似 $title = "Emergency Contact"
和 $important = "true"
的东西。
如何修改我的正则表达式字符串来执行此操作?我真的不知道我在用正则表达式做什么,很惊讶我已经做到了。
还有一点需要注意,并非每个简码都具有这两个值。一些替代示例:
[tab]
[tab title="Emergency Contact"]
[tab important="true"]
您是否在寻找类似的东西:
<?php
$string = 'some gibberish here [tab title="Emergency Contact" important="true"] some additional info here';
$regex = '~
(?:\[tab\s # looks for [tab + whitespace literally
| # or
(?!\A)\G\s) # (?!\A) = negative lookahead
# to make sure the following is not the start
# of the string
# \G matches at the end of the previous match
(?P<key>\w+) # match a word character greedily to group "key"
=
"(?P<value>[^"]+)" # " + anything not " + ", save this to group "value"
~x'; # verbose modifier
preg_match_all($regex, $string, $matches, PREG_SET_ORDER);
foreach ($matches as $match)
echo "Key: {$match['key']}, Value: {$match['value']}\n";
/* output:
Key: title, Value: Emergency Contact
Key: important, Value: true
*/
?>
这将查找 [tab]
标签中的所有 key/value 对,参见 a demo on regex101.com。
额外 demo can be found on ideone.com.
您可以使用 "preg_match_all".
获取简码属性
<?php
//string
$qoute_string = '[tab title="Emergency Contact" important="true"]';
//pattern
$pattern = '/ (.*?)="(.*?)"/';
$tab_attr = array();
//get the attributes.
if(preg_match_all($pattern, $qoute_string, $data)) {
foreach ($data[0] as $key => $val) {
$variable = explode("=",$val);
$tab_attr[trim($variable[0])] = str_replace('"', "", $variable[1]);
}
}
//see the results
print_r($tab_attr);
echo '<br />';
//title
echo $tab_attr['title'];
echo '<br />';
//important
echo $tab_attr['important'];
?>
I'm basically trying to get something like $title = "Emergency Contact" and $important = "true".
尝试[a-z]+="[A-Za-z ]+"
。
这是最简单的正则表达式,将匹配以下格式的模式。
Characters from a-z="Characters from A-Z and a-z including space"
我有以下简码,我正在尝试 运行 a preg_replace,以获取标题值。
[tab title="Emergency Contact" important="true"]
目前,我使用以下过滤器:
$sanitized_title = $tab_title ? preg_replace("/[\”\"\’\']*|(?:’|”)*\]/i", "", preg_replace("/\[tab *title=[\”\"\’\']*|(?:’|”)*/i", "", $tab_title[0])) : __("Open Tab");
这个returns"Emergency Contact important=true",这不是我需要的。我基本上是想得到类似 $title = "Emergency Contact"
和 $important = "true"
的东西。
如何修改我的正则表达式字符串来执行此操作?我真的不知道我在用正则表达式做什么,很惊讶我已经做到了。
还有一点需要注意,并非每个简码都具有这两个值。一些替代示例:
[tab]
[tab title="Emergency Contact"]
[tab important="true"]
您是否在寻找类似的东西:
<?php
$string = 'some gibberish here [tab title="Emergency Contact" important="true"] some additional info here';
$regex = '~
(?:\[tab\s # looks for [tab + whitespace literally
| # or
(?!\A)\G\s) # (?!\A) = negative lookahead
# to make sure the following is not the start
# of the string
# \G matches at the end of the previous match
(?P<key>\w+) # match a word character greedily to group "key"
=
"(?P<value>[^"]+)" # " + anything not " + ", save this to group "value"
~x'; # verbose modifier
preg_match_all($regex, $string, $matches, PREG_SET_ORDER);
foreach ($matches as $match)
echo "Key: {$match['key']}, Value: {$match['value']}\n";
/* output:
Key: title, Value: Emergency Contact
Key: important, Value: true
*/
?>
这将查找 [tab]
标签中的所有 key/value 对,参见 a demo on regex101.com。
额外 demo can be found on ideone.com.
您可以使用 "preg_match_all".
获取简码属性<?php
//string
$qoute_string = '[tab title="Emergency Contact" important="true"]';
//pattern
$pattern = '/ (.*?)="(.*?)"/';
$tab_attr = array();
//get the attributes.
if(preg_match_all($pattern, $qoute_string, $data)) {
foreach ($data[0] as $key => $val) {
$variable = explode("=",$val);
$tab_attr[trim($variable[0])] = str_replace('"', "", $variable[1]);
}
}
//see the results
print_r($tab_attr);
echo '<br />';
//title
echo $tab_attr['title'];
echo '<br />';
//important
echo $tab_attr['important'];
?>
I'm basically trying to get something like $title = "Emergency Contact" and $important = "true".
尝试[a-z]+="[A-Za-z ]+"
。
这是最简单的正则表达式,将匹配以下格式的模式。
Characters from a-z="Characters from A-Z and a-z including space"