1

I just want to replace the following string:

This is my first sentence.
.:.
This is my second sentence.
.:.
This is my third sentence.
.:.

The output should be like:

This is my first sentence.
1
This is my second sentence.
2
This is my third sentence.
3

How can I do it in more efficient and easy way?

1

1 Answer 1

2
echo preg_replace_callback("/@/",
    function(){static $count=0; return ++$count;},
    "A@B@C@");
Sign up to request clarification or add additional context in comments.

4 Comments

All sentences are stored in $string. Should i replace "A@B@C@" with $string?
Yep, and the "/@/" with the actual pattern you are replacing.
I have tried it but it is not working. preg_replace_callback(".:.", function(){static $count=0; return ++$count;}, $Product_description);
That would be a pattern with . as delimiters.... and matching all literal :'s.... I doubt that is what you want. If you are having trouble creating the proper regex, I suggest posing it as a new question.

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.