Pepijn Gieles
Pepijn Gieles

Reputation: 727

PHP: How can I str_replace a query of variable content?

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!

Upvotes: 0

Views: 93

Answers (1)

Narendrasingh Sisodia
Narendrasingh Sisodia

Reputation: 21422

Instead of str_replace you can use preg_replace like as

preg_replace('/(week\s?(\d+))/',"<a href='/?w=$2'>$1</a>",$your_string);

Upvotes: 2

Related Questions