Re: RegExp question: how to add a number?
| From: | David Harkness | Date: | Thu, 14 Oct 2010 23:30:38 +0000 |
| Subject: | Re: RegExp question: how to add a number? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-308727@lists.php.net to get a copy of this message | ||
On Thu, Oct 14, 2010 at 1:42 PM, Andre Polykanine <andre@oire.org> wrote:
> But (attention, here it is!) if the string starts with
> something like "Re[4]:", it should replace it by "Re[5]:".
>
Regular expressions do not support any mathematical operations. Instead, you
need to use preg_match() to extract the number inside the brackets,
increment it, and build a new string using simple concatenation (.).
elseif ($start=="re[") {
if (preg_match('/^re\[(\d+)\](.*)/i', $f['Subject'], $matches) > 0)
{
$f['Subject'] = 'Re[' . ($matches[1] + 1) . ']' .
$matches[2];
}
else {
// no closing brace -- now what?
}
}
David