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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:31:48+00:00 2026-05-23T21:31:48+00:00

I need to extend the functionality of the following code snippet to convert spaces

  • 0

I need to extend the functionality of the following code snippet to convert spaces -only- between PRE tags in a string containing html:

str_replace(' ',' ',$str);

For example, if $str cotained the following string;

<p>abc 123</p>
<pre class="abc" id="123">abcedfg 12345</pre>

it would be converted to:

<p>abc 123</p>
<pre class="abc" id="123">abcedfg&nbsp;12345</pre>

similarly;

<p>abc 123</p>
<pre>abcedfg 12345</pre>

would be converted to:

<p>abc 123</p>
<pre>abcedfg&nbsp;12345</pre>
  • 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-05-23T21:31:50+00:00Added an answer on May 23, 2026 at 9:31 pm
    $text = '<pre>test 1234 123</pre>';
    $text2 = '<pre class="test">test 1234 123</pre>';
    
    function testreplace($text) {
        return preg_replace_callback('/[\<]pre(.*)[\>](.*)[\<]\/pre[\>]/i', 
            create_function(
                '$matches',
                'return "<pre".$matches[1].">".str_replace(" ", "&nbsp;", $matches[2])."</pre>\n";'
            ), $text);
    }
    
    echo testreplace($text);
    echo testreplace($text2);
    

    Took me a while… But it works.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create my own std::string wrapper to extend its functionality. But
I need to create following functionality in javascript: User should be able to get
I have the following code: public static string FormatHostAndUrl(this HttpSessionStateBase session) { var a
I need to extend an element in an XSD schema. In the original schema,
I need to extend a class, which is encapsulated in a closure. This base
Hi I need to extend the CListControl class in C++/MFC, which will add several
Need to an expression that returns only things with an I followed by either
Need to locate the following pattern: The letter I followed by a space then
I need advice on what I need to do to make the following two
Our customer has a requirement to extend the functionality of their existing large government

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.