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 9145629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:33:43+00:00 2026-06-17T10:33:43+00:00

I have this code: function toDataUri( $html ) { # convert css URLs to

  • 0

I have this code:

function toDataUri( $html )
{
  # convert css URLs to data URIs
  $html = preg_replace_callback( "#(url\([\'\"]?)([^\"\'\)]+)([\"\']?\))#", 'create_data_uri', $html );
  return $html;
}

// callback function
private function create_data_uri( $matches )
{
  $filetype = explode( '.', $matches[ 2 ] );
  $filetype = trim(strtolower( $filetype[ count( $filetype ) - 1 ] ));

  // replace ?whatever=value from extensions
  $filetype = preg_replace('#\?.*#', '', $filetype);

  $datauri = $matches[ 2 ];
  $data =  get_file_contents( $datauri );

  if (! $data) return $matches[ 0 ];

  $data = base64_encode( $data );

  //compile and return a data: URI with the encoded image data
  return $matches[ 1 ] . "data:image/$filetype;base64,$data" . $matches[ 3 ];
}

It basically searches for URLs with format url(path) in HTML file and replaces them with base 64 Data URIS.

The problem is that if input html is few kilos such as 10kb, it takes ages to return the final response. Is there any optimization we can do in such case or any other solution you have that when given html, it searches for url(path) matches and converts them to data uris ?

  • 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-17T10:33:44+00:00Added an answer on June 17, 2026 at 10:33 am

    The expression is cheap already — starts with a fixed string and doesn’t need backtracking.

    In PCRE there’s S modifier that enables some regex optimisation, but it matters only for patterns without a fixed prefix.

    It shouldn’t be slow — 10KB isn’t much for a simple regex like this. Perhaps the bottleneck is somewhere else?

    • If you have unclosed url( in the parsed file and no ) to the end of the file, then it’ll scan a bit more. [^\"\'\)]{0,1000} would limit that. But it’s a minor optimisation that only makes a difference when you have pathological syntax errors in the file.
    • You can remove () around whole expression. 0th match is always capturing entire string.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have this code $(function() { $('#ans_vote a span').click(function(){alert('working');return false;});}); and this html <div
I have this code snippet: $(function() { $('.toolbar [id^=button]').on('click', function () { $(this) .css('background-color',
Hi I have this JS CODE function openPopup(url) { var newElem = new Element(
I have this code: function getTime(zone, success) { var url = 'http://json-time.appspot.com/time.json?tz=' + zone,
I have this code function test(){}; test.prototype.testMethod = function(){return 1;} var t = new
i have this code function, im trying to get an html and appendchild a
I have this code: function changeLogo() { var changeLogo = $(#logo).css(z-index, 8); } function
I have this code: function EnableiPad(event) { var id = $(event.currentTarget).attr(data-id); var status =
I have this code function check(){ $.get(friend.php, { id: friendid }, function(data){ //Do stuff
I have this code: function some_object(...) { this.data = {...}; this.do_something = function(...) {

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.