PHP preg_replace_callback 的正则表达式

发布于 2024-08-24 06:34:54 字数 835 浏览 8 评论 0原文

正则表达式不是我的菜。 :(

我有一个可能包含多个子字符串的字符串,例如:

[var1="111" var2="222" var3="222" var4="444"]

我基本上需要将每个出现的地方替换为获取每个变量的函数的结果。

$string = '[var1="111" var2="222" var3="333" var4="444"]';    
$regex = 'var1="(.*)" var2="(.*)" var3="(.*)" var4="(.*)"';

function doit($matches){
    return "<strong>".implode(", ", $matches) ."</strong>";
}

echo preg_replace_callback($regex,'doit',$string);

我期望 111, 222, 333, 444.

但我没有感受到任何帮助!

编辑:一些澄清..

我只是为了这个问题而简化

  1. “vars”实际上并没有。是 称为“var1”,“var2”更像 [recID =“22”模板=“theName” option="some value", type="list"]

  2. 回调会更多 复杂然后我简单 内爆...我会使用“id”和 连接到数据库,获取 记录并将它们插入到 模板...因此需要 回电...

  3. 匹配的值可能几乎 任何东西,而不仅仅是数字。

Regular expressions is just not my thing. :(

I have a string that may contain multiple sub strings such as:

[var1="111" var2="222" var3="222" var4="444"]

I basically need to replace each occurrence with the results of a function that gets each variable.

$string = '[var1="111" var2="222" var3="333" var4="444"]';    
$regex = 'var1="(.*)" var2="(.*)" var3="(.*)" var4="(.*)"';

function doit($matches){
    return "<strong>".implode(", ", $matches) ."</strong>";
}

echo preg_replace_callback($regex,'doit',$string);

I’m expecting 111, 222, 333, 444.

But I’m not feeling the love. Any help would be appreciated!

Edit: some clarification..

I was simplifying for the sake of the question.

  1. the "vars" will not really be
    called "var1", "var2" more like
    [recID="22" template="theName"
    option="some value", type="list"]

  2. the call back would be more
    complicated then my simple
    implode... I would use "id" and
    connect to a database, get the
    records and insert them into the
    template... Hence the need for the
    call back...

  3. matched values could be almost
    anything, not just numbers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

赠意 2024-08-31 06:34:54

我会分两步完成:

  1. 获取标签
  2. 获取每个标签的属性

这是一个示例:

function doit($match) {
    $parts = preg_split('/(\w+="[^"]*")/', $match[1], -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
    foreach ($parts as $part) {
        if (trim($part) !== "") {
            if (!preg_match('/^(\w+)="([^"]*)"$/', $part, $match)) {
                // syntax error
            } else {
                var_dump($match);
            }
        }
    }
}

$str = '[var1="111" var2="222" var3="333" var4="444"]';
$re = '/\[((?:[^"[\]]+|"[^"]*")*)]/';
preg_replace_callback($re, 'doit', $str);

通过这个,您甚至可以检测语法错误。

这不是一个完美的解决方案,因为逐字符读取输入并根据它们所在的上下文确定标记会更有效。

I would do it in two step:

  1. Get the tags
  2. Get the attributes for each tag

Here’s an example:

function doit($match) {
    $parts = preg_split('/(\w+="[^"]*")/', $match[1], -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
    foreach ($parts as $part) {
        if (trim($part) !== "") {
            if (!preg_match('/^(\w+)="([^"]*)"$/', $part, $match)) {
                // syntax error
            } else {
                var_dump($match);
            }
        }
    }
}

$str = '[var1="111" var2="222" var3="333" var4="444"]';
$re = '/\[((?:[^"[\]]+|"[^"]*")*)]/';
preg_replace_callback($re, 'doit', $str);

With this you can even detect syntax errors.

It’s not a perfect solution as it would be more efficient to read the input character by character and determine the tokens depending on the contexts they are found in.

不气馁 2024-08-31 06:34:54

另一个答案可能是更好的解决方案,但以下是如何使用 preg_replace_callback 来做到这一点:

$string = '[var1="111" var2="222" var3="333" var4="444"]';

// Note: regular expressions need to start and end with /
$regex = '/var1="(.*)" var2="(.*)" var3="(.*)" var4="(.*)"/';

function doit($matches){
    // The first element of matches is the complete
    // match, shift is here just to get rid of it
    // (probably a better way to do this)
    array_shift($matches);
    return "<strong>".implode(", ", $matches) ."</strong>";
}
echo preg_replace_callback($regex,'doit',$string);

The other answer may be a better solution, but here is how you could do it with preg_replace_callback:

$string = '[var1="111" var2="222" var3="333" var4="444"]';

// Note: regular expressions need to start and end with /
$regex = '/var1="(.*)" var2="(.*)" var3="(.*)" var4="(.*)"/';

function doit($matches){
    // The first element of matches is the complete
    // match, shift is here just to get rid of it
    // (probably a better way to do this)
    array_shift($matches);
    return "<strong>".implode(", ", $matches) ."</strong>";
}
echo preg_replace_callback($regex,'doit',$string);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文