1

I need to do use php/regex to replace this:

{{image-4-m}}

to this:

<img src="4.jpg" class="m" />

or another example, this:

{{image-53-v}}

to this:

<img src="53.jpg" class="v" />

How would I accomplish this?

1 Answer 1

3

You can reference groups by index with preg_replace by using $n.

preg_replace('/{{image-(\d+)-([mv])}}/', '<img src="$1.jpg" class="$2" />', $template);

Here's a demo.

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

Comments

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.