Jared Eitnier
Jared Eitnier

Reputation: 7152

php recursive insert string into string

I have a huge string in PHP. In it are many url strings such as:

background: #9dc9de url("@{base-url}/img/background.jpg") center top no-repeat;

What is the best method to go through the entire string, find any url strings and insert a hash code before the extension based on the current timestamp to get a result such as:

background: #9dc9de url("@{base-url}/img/background_013857308.jpg") center top no-repeat;

I'm currently trying to explode based on a few parameters but it's definitely not a clean idea.

Upvotes: 2

Views: 95

Answers (2)

Jared Eitnier
Jared Eitnier

Reputation: 7152

I came up with preg_match_all('/\("([^"]+)"\)/', $content, $matches); which matches anything inside parentheses and double-quotes.

Upvotes: 0

Marcelo Pascual
Marcelo Pascual

Reputation: 820

You should't use that approach in a production server, as you will call all files everytime instead of fetching them from cache. But it could be a good idea for a development server.

I would use:

$tm = time();
$css = str_ireplace( array('.jpg', '.gif', '.png'), array('.jpg?t='.$tm, '.gif?t='.$tm, '.png?t='.$tm), $css );

Upvotes: 1

Related Questions