php新手问题:正则表达式

发布于 2024-10-28 14:25:20 字数 581 浏览 9 评论 0原文

抱歉,我已经提出了这个要求,但尚未找到解决方案:( 这是我的字符串:(如您所见,它有换行符)

Webname:    [webname]
Username:   [username]
IP:     [IP]

我需要读出方括号内的值。 这是我的代码:

    $pattern = '/\[(.|\n)+?\]/'; // i've used the same syntax for my asp projects, always worked
    preg_match($pattern, $txt, $matches, PREG_OFFSET_CAPTURE);
    echo "matches:".count($matches)."\n\n";
    foreach ($matches as $match) 
    {
        echo $match[0]."\n";
    }

我只得到 2 个匹配项:[webname]e (???) 我已经摆弄这个好几个小时了,无法找出问题所在..

有什么想法吗? 谢谢

i'm sorry i've already asked for this but couldn't find a solution yet :(
here's my string: (as you can see it has linebreaks)

Webname:    [webname]
Username:   [username]
IP:     [IP]

i need to read out the values inside the square brackets.
here's my code:

    $pattern = '/\[(.|\n)+?\]/'; // i've used the same syntax for my asp projects, always worked
    preg_match($pattern, $txt, $matches, PREG_OFFSET_CAPTURE);
    echo "matches:".count($matches)."\n\n";
    foreach ($matches as $match) 
    {
        echo $match[0]."\n";
    }

i'm getting only 2 matches: [webname] and e (???)
i'm fiddling with this for hours now and can't find out what's wrong ..

any ideas?
thanks

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

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

发布评论

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

评论(4

深空失忆 2024-11-04 14:25:20

看起来比实际情况更复杂。换行符在这里不起作用。

$pattern = '/\[(.+?)\]/';
preg_match_all($pattern, $txt, $matches);

print_r($matches);

给出

Array
(
    [0] => Array
        (
            [0] => [webname]
            [1] => [username]
            [2] => [IP]
        )

    [1] => Array
        (
            [0] => webname
            [1] => username
            [2] => IP
        )

)

所以值将在 $matches[1] 中。如果您想要包含括号的值 ($matches[0]),您也可以在模式中省略括号。

Looks more complicated than it has to be. Line breaks don't play any role here.

$pattern = '/\[(.+?)\]/';
preg_match_all($pattern, $txt, $matches);

print_r($matches);

gives

Array
(
    [0] => Array
        (
            [0] => [webname]
            [1] => [username]
            [2] => [IP]
        )

    [1] => Array
        (
            [0] => webname
            [1] => username
            [2] => IP
        )

)

So the values would be in $matches[1]. If you want the values including the brackets ($matches[0]), you can also omit the parenthesis in the pattern.

人生戏 2024-11-04 14:25:20

第一个捕获组(在您的情况下只有一个)位于 $matches[1]

The first capture group, in your case there is only one, is in $matches[1]

把时间冻结 2024-11-04 14:25:20

尝试类似的方法

$pattern = '/\[([^\]]+)\]/';

并使用 preg_match_all() 来获取所有匹配项。

Try something like

$pattern = '/\[([^\]]+)\]/';

and use preg_match_all() to get all matches.

赠意 2024-11-04 14:25:20

试试这个

$values = array();

foreach (explode("\n", $text) as $line) {
    if (preg_match('/([^:]++):[^\s]*+(.*+)/', $line, $match)) {
        $values[$match[1]] = $match[2];
    }
}

var_dump($values);

Try this

$values = array();

foreach (explode("\n", $text) as $line) {
    if (preg_match('/([^:]++):[^\s]*+(.*+)/', $line, $match)) {
        $values[$match[1]] = $match[2];
    }
}

var_dump($values);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文