0

So this is a preg_replace associated question i guess,

I have a string with multiple repeating patterns

they all formated as:

some string :22: more text :12: etc

how do i replace the ":" around them with some different char?

0

3 Answers 3

1

You can do something like this:

$string = 'some string :22: more text :12: etc';
$regex = '/:(\d+):/';
$newString = preg_replace($regex, "@$1@", $string);

Note: You have to replace the '@' in the second parameter with the char you want (also different chars before and after the numbers).

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

Comments

0

Sbustitudes _ for : around numbers:

preg_replace('/:(\d+):/', '_$1_', 'some string :22: more text :12: etc');

Comments

0

EDIT: Misunderstood original question. However, is still a flexible option:

$result = str_replace(":22:", "tag", "some string :22: more text :12: etc");
$result = str_replace(":12:", "other_tag", $result);

Replace the ? character with your replacement character.

2 Comments

i'd like to replace like: "text 22 moretext" diffrent values on both sides
Ah. That is more clear than your original question. Then @Aurelio is more correct.

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.