I have one sentence like this:
{pattern} test {pattern} how r u {pattern}
How can I replace {pattern} with different values like
{AAA} test {BBB} how r u {CCC}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you wish to replace the same pattern with something else every time you could consider using
preg_replace_callback(). At every match, a function is executed and you can return a different string at every invocation:This solution cycles the replacement strings, so it will replace like AAA, BBB, CCC, AAA (again), etc. The exact strategy you wish to adopt may be different.
The second parameter to
preg_replace_callback()may also be a closure (>= 5.3)Also, instead of using a regular function with
staticdeclarations, it might be more appropriate to use an object for state management.