[英]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
工作,但不幸的是,这超出了我目前的知识,因此将不胜感激。
<?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
\[ # 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.