1

I'm trying to create a preg_replace pattern to add sequentially increasing numbers in replacements to words. For example, I want to change this:

"Mary had a little lamb"

to this:

"(:word_1:Mary:) (:word_2:had:) (:word_3:a:) (:word_4:little:) (:word_5:lamb:)"

The longest lines would never have more than 16 words. I don't think it would be such a problem, except I can't find anything on sequentially increasing numbers. After much frustration, I thought about just writing an expression that would just make 16 seperate (recursive?) passes through each line, but that seemed kind of insane. Any help would be greatly appreciated.

Thanks!

EDIT:

Basically I just need to prepend each word in a string with "(:word_1:", "(:word_2:", etc., each one increasing incrementally.

7
  • Does it have to be a regex? Commented Sep 26, 2017 at 12:40
  • regexps alone, although powerful, do not have the processing capability required to increment a number, you'd have to use preg_replace_callback() to that end Commented Sep 26, 2017 at 12:41
  • Can't you use explode on this? Commented Sep 26, 2017 at 12:41
  • 4
    How's this? Commented Sep 26, 2017 at 12:42
  • I'm working within the pmwiki framework, and they have an ROSpattern (replace on save) function that specifically uses preg_replace. I'm actually not sure if I could use preg_replace_callback or not, but maybe. Commented Sep 26, 2017 at 12:48

1 Answer 1

1

Here is your solution. Try this

<?php
$string = "Mary had a little lamb";
$count = 0;
$newstring = preg_replace_callback(
    '/\S+/',
    function($match) use (&$count) { return (( '(:word_' . $count++ . ':' .$match[0] . ':) ' )); },
    $string
    );
echo $newstring;
?>

PHP preg_replace_callback function is matching the regex pattern given as the first parameter with the $string given as its third parameter. you can also modify the regex pattern as per your need to match your required value. I also use $count as passing by reference to get the count of matched words.

Sign up to request clarification or add additional context in comments.

5 Comments

This looks good, but can you add some comments as to what exactly is happening?
Use either ++$count or start $count from 1, I think OP wants it to start from 1.
Thanks, both of you, but I'm afraid I can't use preg_replace_callback, only preg_replace, because I'm working with PmWiki software, and they have an "ROSpattern" (Replace On Save) function that is limited to preg_replace.
PHP preg_replace_callback function is matching the regex pattern given as the first parameter with the $string given as its third parameter. you can also modify the regex pattern as per your need to match your required value. I also use $count as passing by reference to get the count of matched words.
OK, that helps, thanks! I still have to mess with it because of limitations with pmwiki, but I think I can get there now. Appreciate all your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.