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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:14:15+00:00 2026-06-12T06:14:15+00:00

I am trying to minify an HTML string but also wrap the text at

  • 0

I am trying to minify an HTML string but also wrap the text at a certain character count. So I need a special function when the HTML string contains embedded CSS. I have tried using the php function “wordwrap” but its wrapping text within double quotes (“) and single quotes (‘) which breaks the display. For example, if we had the following string:

font-family: Arial, 'Helvetica Nueue', 'Times Roman'

Now if we do a wordwrap on this at say, 31, it will become:

font-family: Arial, 'Helvetica
 Nueue', 'Times Roman'

And this will cause browsers to misinterpret the CSS string when loaded.

Does anyone know how or has written a wordwrap function that won’t wrap when the text is contained within quotes (single or double)?

Thanks!

UPDATE

Sorry for asking for just code. This is what I envision being done (which I am currently working on programming):

Loop through the CSS string and grab chunks of the string that are inbetween ; } or { if its close the break limit, add the substrings to an array. Once the loop is complete implode the array into a string using “\n” as the glue.

Anyone have a better/different approach?

  • 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-12T06:14:17+00:00Added an answer on June 12, 2026 at 6:14 am

    The PHP wordwrap function has some knowledge about specific ASCII characters.

    In your example text you do not want to allow the spaces inside of single quotes to wrap, so you can just replace spaces with non-breaking spaces. wordwrap will then not wrap at those any longer:

    $nowrap = function ($string) {
        return str_replace(' ', "\xA0", $string);
    };
    

    This is a ASCII version for such a “nowrap” function.

    As it only needs to be applied partially to the single-quoted parts, those parts need to be obtained. This can be easily done with a regular expression:

    /'[^']+'/
    

    To apply the nowrap function onto the matches however, this needs a helper function to map the first matching group to the input value:

    $first = function ($callback) {
        return function ($params) use ($callback) {
            return $callback($params[0]);
        };
    };
    

    Then this can be easily applied:

    $quotes  = "/'[^']+'/";
    $subject = preg_replace_callback($quotes, $first($nowrap), $subject);
    

    You can then safely apply the wordwrap function which for the example wordwrap($subject, 31) will give the following output:

    font-family: Arial,
    'Helvetica Nueue',
    'Times Roman'
    

    You can also convert those back if you do not want to have these in your output.

    The code in full:

    $subject = "font-family: Arial, 'Helvetica Nueue', 'Times Roman'";
    
    $nowrap = function ($string) {
        return str_replace(' ', "\xA0", $string);
    };
    
    $first = function ($callback) {
        return function ($params) use ($callback) {
            return $callback($params[0]);
        };
    };
    
    
    $quotes  = "/'[^']+'/";
    $subject = preg_replace_callback($quotes, $first($nowrap), $subject);
    
    
    echo wordwrap($subject, 31);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use Reducisaurus Web Service to minify CSS and Javascript but I've
I am trying to minify a third-party JavaScript library using Google Closure Compiler, but
trying to get my first HTML/CSS uni assignment sorted but have become a bit
I'm trying to minify multiple CSS files using preg_replace() . Actually, I'm only trying
Trying to get a wildcard search to pick up on any text in org_name
trying to figure out why this is happening - I have an input text
Trying to load a page fragment, or at least jump to a div but
I am trying to minify some files in a roll-up script using jsmin. This
Trying to find some information on this but am unable to get any results
Trying to execute a Powershell cmdlet from a MVC 3 Controller using impersonation but

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.