2

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.

10
  • Please define 'efficient' in this context. There are many solutions available, but we cannot make a subjective determination about which one will be the most suitable to your situation. Commented May 20, 2013 at 20:44
  • Efficient in terms of performance. The application will grow over time so the string being manipulated will grow. Commented May 20, 2013 at 20:47
  • Are you planning to parse the files by hand? If so, what editor will you use? If not, do you plan to parse the CSS via PHP? If so, what algorithm will you use to create the hashes? If not PHP, what? Your question does not contain enough information for us to provide specific answers tailored to your situation. Commented May 20, 2013 at 20:48
  • I have all of this figured out already. I'm minifying and combining css files on the fly. Before I output the string buffer I wanted to run through the string and insert a hash code based on the current timestamp. Codekit parses my LESS files for me but I don't know how that's relevant to the question. Commented May 20, 2013 at 20:51
  • I simplified my request if it helps. Maybe I overcomplicated it before. Commented May 20, 2013 at 20:58

2 Answers 2

1

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 );
Sign up to request clarification or add additional context in comments.

1 Comment

The whole reason behind my intent is for caching purposes so I'm with you there.
0

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

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.