简体   繁体   English

使用preg_replace删除除引号之间的内容之外的所有内容

[英]Using preg_replace to remove everything except content between quotes

I've got the following shortcode that I'm trying to run a preg_replace on, to get the title value. 我有以下短代码,我正在尝试运行preg_replace,以获取标题值。

[tab title="Emergency Contact" important="true"]

Currently, I use the below filter: 目前,我使用以下过滤器:

$sanitized_title = $tab_title ? preg_replace("/[\”\"\’\']*|(?:’|”)*\]/i", "", preg_replace("/\[tab *title=[\”\"\’\']*|(?:’|”)*/i", "", $tab_title[0])) : __("Open Tab");

This returns "Emergency Contact important=true", which isn't what I need. 这将返回“Emergency Contact important = true”,这不是我需要的。 I'm basically trying to get something like $title = "Emergency Contact" and $important = "true" . 我基本上试图获得类似$title = "Emergency Contact"$important = "true"

How can I modify my regex string to do this? 如何修改我的正则表达式字符串来执行此操作? I really don't know what I'm doing with regex, surprised I've gotten as far as I have. 我真的不知道我正在做什么与正则表达式,惊讶我已经得到了尽可能多。

One additional thing to note, not every shortcode will have both values. 还有一点需要注意,并非每个短代码都有两个值。 Some alternate examples: 一些替代的例子:

[tab]
[tab title="Emergency Contact"]
[tab important="true"]

Are you looking for something like: 你在寻找类似的东西:

<?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
*/
?>

This will look for all key/value pairs in the [tab] tag, see a demo on regex101.com . 这将查找[tab]标签中的所有键/值对,请参阅regex101.com上的演示
An additional demo can be found on ideone.com . 可以在ideone.com上找到其他演示

I'm basically trying to get something like $title = "Emergency Contact" and $important = "true". 我基本上试图获得类似$ title =“Emergency Contact”和$ important =“true”的内容。

Try [az]+="[A-Za-z ]+" . 试试[az]+="[A-Za-z ]+"

This is the simplest regex which will match pattern of following format. 这是最简单的正则表达式,它将匹配以下格式的模式。

Characters from az="Characters from AZ and az including space"

Regex101 Demo Regex101演示

You can get the shortcode attributes with using "preg_match_all". 您可以使用“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'];
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM