简体   繁体   English

使用php preg_match从论坛报价中获取ID

[英]Using php preg_match to get an id from forum quotes

I've been using preg_match to try and get an id from a quote in my forums I have written. 我一直在使用preg_match尝试从我撰写的论坛中的报价中获取ID。 This is what I have so far. 到目前为止,这就是我所拥有的。

$quote = '[quote]19[\quote] This is a reply to the quote.';

$get = '/([quote])([0-9])([\quote])/';

$id = '';

preg_match($get, $quote, $id);

echo $id[0];

Unfortunately this doesn't give me the result I was hoping for and I have tried many variations and even tried preg_replace in hopes that might give me what I need but After a lot of reading on stack overflow I think preg_match is the way to go. 不幸的是,这并没有给我我想要的结果,我尝试了很多变化,甚至尝试了preg_replace ,希望可能会给我我所需要的东西,但是在大量阅读了堆栈溢出后,我认为preg_match是必经之路。 I just can't seem to get what I want which is the id in between the quote tags. 我只是似乎无法得到我想要的是引号标签之间的ID。

My experience with preg is limited at best and I've tried my best to try to get it to work but unfortunately it is beyond my current knowledge so any assistance would be appreciated. 我对preg经验充其量是有限的,我尽了最大的努力使preg工作,但不幸的是,这超出了我目前的知识,因此将不胜感激。

Hints 提示

  • [ and ] are used as character class delimiters. []用作字符类定界符。
    They must be escaped \\[, \\] when to be taken literally. 从字面上看时,它们必须转义\\ [,\\]。
    The definition [0-9] means exactly this: the character class of digits. 定义[0-9]的确切含义是:数字字符类。
  • (…) brackets embrace the result group. (…)方括号包含结果组。
    If you wish to extract the numeric data between [quote] and [\\quote] only ([0-9]*?) should be in brackets. 如果希望提取[quote]和[\\ quote]之间的数字数据,则应将([0-9] *?)放在方括号中。 The result would then be in $id[1] (group # 1). 结果将在$ id [1](组#1)中。
  • The backslash "\\" character in [\\quote] must be escaped too, as it is the escape character itself: [\\\\\\\\quote] (4 times \\, since it is interpreted twice; somehow tricky, I know). [\\ quote]中的反斜杠“ \\”字符也必须转义,因为它本身就是转义字符:[\\\\\\\\ quote](4倍\\,因为它被解释了两次;我知道有点棘手)。
    Btw: maybe [/quote] is meant; 顺便说一句:也许[/ quote]是故意的; that would make it easier (?) 这样会更容易(?)

Code

<?php
    $quote1 = '[quote]19[\quote] This is a reply to the quote.';
    $quote2 = '[quote]19[/quote] This is a reply to the quote.';
    // $get = '/\[quote\]([0-9].*?)\[\quote\]/';
    $get1 = '%\[quote\]([0-9]*?)\[\\\\quote\]%';
    $get2 = '%\[quote\]([0-9]*?)\[/quote\]%';
    $id = '';
    preg_match($get1, $quote1, $id);
    echo '$get1,$quote1 :: ' . $id[1] . '<br />';
    preg_match($get2, $quote2, $id);
    echo '$get2,$quote2 :: ' . $id[1] . '<br />';
?>

Output: 输出:
$get1,$quote1 :: 19 $ get1,$ quote1 :: 19
$get2,$quote2 :: 19 $ get2,$ quote2 :: 19

Regex commented 正则表达式评论

    \[          # Match the character “[” literally
    quote       # Match the characters “quote” literally
    \]          # Match the character “]” literally
    (           # Match the regular expression below and capture its match into backreference number 1
       [0-9]       # Match a single character in the range between “0” and “9”
          *?          # Between zero and unlimited times, as few times as possible, expanding as needed (lazy)
    )
    \[          # Match the character “[” literally
        \\\\       # Match the character “\” literally
    quote       # Match the characters “quote” literally
    \]          # Match the character “]” literally

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

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