Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7725151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:52:05+00:00 2026-06-01T04:52:05+00:00

Imagine I have a string, $my_css_string , and we know it contains CSS styling.

  • 0

Imagine I have a string, $my_css_string, and we know it contains CSS styling.

Now imagine I have a function string url_replace(string $url) that returns a modified URL that I want to replace every url() with in $my_css_string.

So, for simplicity’s sake, let’s say url_replace() ALWAYS returns the string "xxxx".

How can you go through $my_css_string, find each url() in the string, and replace that URL with what you get from url_replace()?


So, if you had this CSS as $my_css_string:

@import url('http://example.com/css/animals.css');
.dog {
    background:url("/images/dog.png");
    border:1px solid rgb(0,0,128);
}
.cat {
    background:url(http://example.com/images/cat.gif);
}

If you ran the code, $my_css_string would become:

@import url('xxxx');
.dog {
    background:url( "xxxx" );
    border:1px solid rgb(0,0,128);
}
.cat {
    background:url(xxxx);
}

Notice how it needs to handle multiple ways of defining URLs.

How do I do that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T04:52:06+00:00Added an answer on June 1, 2026 at 4:52 am

    You want to use preg_replace_callback:

    $my_new_css_string = preg_replace_callback(
                            '/background-image:\s*url\(\s*([\'"]*)(?P<file>[^\1]+)\1\s*\)/i',
                            'url_replace',
                            $my_css_string);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

ok imagine that we have text like this String s=123 45678 91011122314 1516; now
Imagine someone coding the following: string s = SomeString; s.ToUpper(); We all know that
Imagine that I have a Spring MVC controller something like this: @Controller @RequestMapping(/base-url) public
Imagine I have an entity: public class MyObject { public string Name { get;
Imagine you have an entity that has some relations with other entities and you
Imagine you have a large dataset that may or may not be filtered by
Imagine I have this code: var myFunc1 = function(event) { alert(1); } var myFunc2
I'm using StructureMap for my DI. Imagine I have a class that takes 1
Imagine that I have a C# application that edits text files. The technique employed
Imagine that I have a Debtor class. With Hibernate, I will define the class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.