preg_replace help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
boinnk
Forum Newbie
Posts: 22
Joined: Fri Jul 26, 2002 9:53 am
Location: Sweden

preg_replace help

Post by boinnk »

Is there a way to count the number of matches in preg_replace. Something like

Code: Select all

<?php

$out = preg_replace("a","a \\something","aaaaaaaaa");

?>
So that $out would contain "a1a2a3a4...".
Is this possible with using only regexp?
User avatar
daven
Forum Contributor
Posts: 332
Joined: Tue Dec 17, 2002 1:29 pm
Location: Gaithersburg, MD
Contact:

Post by daven »

not really. You can do a preg_match(), followed by preg_replace, however.

$out=preg_match("/a/","baaabaaabaaabaaa");
$end=preg_replace("/a/","/a replacement/","baaabaaabaaabaaa");
Post Reply