Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #6992 > unrolled thread
| Started by | Jason C <jwcarlton@gmail.com> |
|---|---|
| First post | 2012-09-09 03:31 -0700 |
| Last post | 2012-09-09 07:16 -0700 |
| Articles | 3 — 3 participants |
Back to article view | Back to comp.lang.php
Dynamic backreferences, finding last in a group Jason C <jwcarlton@gmail.com> - 2012-09-09 03:31 -0700
Re: Dynamic backreferences, finding last in a group Anders Wegge Keller <wegge@wegge.dk> - 2012-09-09 13:51 +0200
Re: Dynamic backreferences, finding last in a group Scott Johnson <noonehome@chalupasworld.com> - 2012-09-09 07:16 -0700
| From | Jason C <jwcarlton@gmail.com> |
|---|---|
| Date | 2012-09-09 03:31 -0700 |
| Subject | Dynamic backreferences, finding last in a group |
| Message-ID | <7ebf5417-2d22-4585-b50a-ba661025ae36@googlegroups.com> |
I'm working with preg_replace(), like so: $pattern = array(); foreach ($filter as $org) array_push($pattern, "/((<br>|\s)*)$org(er|in|...)/i"); $text = preg_replace($pattern, '$1$new$3', $text); The issue I'm having is when $org contains a regex expression, like: (D|d)ear in which case (er|in|...) would now be $4 instead of $3. The question is, is there a way that I can dynamically find the last backreference in the pattern, instead of referring to it strictly by number? If not, I could always check to see if $org contains a (...), and then set the number accordingly. But this is in a foreach loop, and putting an strpos() in a loop like that seems slow; and then, I'm not sure that a backreference like this would works. Like: if (strpos...) $num = "4"; else $num = "3"; $text = preg_replace($pattern, '$1$new$$num', $text); Any suggestions?
[toc] | [next] | [standalone]
| From | Anders Wegge Keller <wegge@wegge.dk> |
|---|---|
| Date | 2012-09-09 13:51 +0200 |
| Message-ID | <87bohffm75.fsf@huddi.jernurt.dk> |
| In reply to | #6992 |
Jason C <jwcarlton@gmail.com> writes: > Any suggestions? Read the manual on regexp syntax, and use named patterns. -- /Wegge Leder efter redundant peering af dk.*,linux.debian.*
[toc] | [prev] | [next] | [standalone]
| From | Scott Johnson <noonehome@chalupasworld.com> |
|---|---|
| Date | 2012-09-09 07:16 -0700 |
| Message-ID | <k2i8cd$ecm$1@dont-email.me> |
| In reply to | #6993 |
On 9/9/2012 4:51 AM, Anders Wegge Keller wrote:
> Jason C <jwcarlton@gmail.com> writes:
>
>> Any suggestions?
>
> Read the manual on regexp syntax, and use named patterns.
>
Wow that is a very good feature I never know about.
This I can imagine will come in very handy.
Thanks
Scotty
Example #4 Using named subpattern
<?php
$str = 'foobar: 2008';
preg_match('/(?P<name>\w+): (?P<digit>\d+)/', $str, $matches);
/* This also works in PHP 5.2.2 (PCRE 7.0) and later, however
* the above form is recommended for backwards compatibility */
// preg_match('/(?<name>\w+): (?<digit>\d+)/', $str, $matches);
print_r($matches);
?>
The above example will output:
Array
(
[0] => foobar: 2008
[name] => foobar
[1] => foobar
[digit] => 2008
[2] => 2008
)
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.php
csiph-web