0

Goal

I'm looking for a way to str_replace variable content. So that I can change this:

...and in week 1 we saw that...

into this:

...and in <a href="/?w=1">week 1</a> we saw that...

Thoughts

I know how str_replace works and I could solve it by just running a hundred different queries, each with another week. But what I want is a query that looks for this:

'week ' + X

So a query that finds every string 'week ' that is followed by a number. Can anyone help me solve this? :)

Tx!

1
  • 2
    Look at preg_replace() and then you can search for your: week \d+ and replace it with what you want. (If you get stuck you can always post the code here and I will help you further :) Commented Nov 21, 2015 at 10:09

1 Answer 1

2

Instead of str_replace you can use preg_replace like as

preg_replace('/(week\s?(\d+))/',"<a href='/?w=$2'>$1</a>",$your_string);
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.