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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:02:43+00:00 2026-06-06T17:02:43+00:00

Maybe you guys can help: I have a variable called $bio with bio data.

  • 0

Maybe you guys can help:

I have a variable called $bio with bio data.

$bio = "Hello, I am John, I'm 25, I like fast cars and boats. I work as a blogger and I'm way cooler then the author of the question";

I search the $bio using a set of functions to search for a certain word, lets say “author” which adds a span class around that word, and I get:

$bio = "Hello, I am John, I'm 25, I like fast cars and boats. I work as a blogger and I'm way cooler then the <span class=\"highlight\">author</span> of the question";

I use a function to limit the text to 85 chars:

$bio = limit_text($bio,85);

The problem is when there are more then 80 chars before the word “author” in $bio.
When the limit_text() is applied, I won’t see the highlighted word author.

What I need is for the limit_text() function to work as normal, adding all the words that contain the span class highlight at the end.
Something like this:

*"This is the limited text to 85 chars, but there are no words with the span class highlight so I am putting to be continued ... **author**, **author2** (and all the other words that have a span class highlight around them separate by comma "*

Hope you understood what I mean, if not, please comment and I’ll try to explain better.

Here is my limit_text() function:

function limit_text($text, $length){ // Limit Text
        if(strlen($text) > $length) {
        $stringCut = substr($text, 0, $length);
        $text = substr($stringCut, 0, strrpos($stringCut, ' '));
        }
        return $text;
    }

UPDATE:

$xturnons = str_replace(",", ", ", $xturnons);
$xbio = str_replace(",", ", ", $xbio);

$xbio = customHighlights($xbio,$toHighlight); 
$xturnons = customHighlights($xturnons,$toHighlight);

$xbio = limit_text($xbio,85);
$xturnons = limit_text($xturnons,85);

The customHighlights function which adds the span class highlighted:

function addRegEx($word){ // Highlight Words
        return "/" . $word . '[^ ,\,,.,?,\.]*/i';
    }
    function highlight($word){
        return "<span class='highlighted'>".$word[0]."</span>";
    }
    function customHighlights($searchString,$toHighlight){
        $searchFor = array_map('addRegEx',$toHighlight);
        $result = preg_replace_callback($searchFor,'highlight',$searchString);
        return $result;
    }
  • 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-06T17:02:44+00:00Added an answer on June 6, 2026 at 5:02 pm

    First, you need to make sure you don’t break words apart by shortening the string. Then you need to append all of the <span class="highlight"> tokens to the end of the shortened string. Here is what I came up with (in about 8 lines!):

    function limit_text($text, $length){
        if( strlen( $text) < $length) {
            return $text;
        }
    
        // Truncate the string without breaking words
        list( $wrapped) = explode("\n", wordwrap( $text, $length));
    
        // Get the span of text occurring after the wrapped string
        $remainder = substr( $text, strlen( $wrapped));
    
        // Add the "to be continued" to $wrapped
        $wrapped .= ' to be continued ... ';
    
        // Now, grab all of the <span class="highlight"></span> tags in the $remainder
        preg_match_all( '#<span class="highlight">[^<]+</span>#i', $remainder, $matches);
    
        // Add the <span> tags to the end of the string, separated by a comma, if present
        $wrapped .= implode( ', ', $matches[0]);
    
        return $wrapped;
    }
    

    Now, with your original test:

    $bio = "Hello, I am John, I'm 25, I like fast cars and boats. I work as a blogger and I'm way cooler then the <span class=\"highlight\">author</span> of the question";
    $bio = limit_text( $bio,85);
    var_dump( htmlentities( $bio));
    

    This outputs:

    string(165) "Hello, I am John, I'm 25, I like fast cars and boats. I work as a blogger and I'm way to be continued ... <span class="highlight">author</span>"
    

    Now, another test with multiple <span> tags:

    This outputs:

    $bio = 'Hello, what about a <span class="highlight">span tag</span> before the limit? Or what if I have many <span class="highlight">span tags</span> <span class="highlight">after</span> <span class="highlight">the</span> limit?';
    $bio = limit_text( $bio,85);
    var_dump( htmlentities( $bio));
    
    string(308) "Hello, what about a <span class="highlight">span tag</span> before the limit? Or what to be continued ... <span class="highlight">span tags</span>, <span class="highlight">after</span>, <span class="highlight">the</span>" 
    

    If you have more test cases, or have a modification to the function above, let me know and I can fix it!

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

Sidebar

Related Questions

Hello guys maybe you maybe can help . here is my problem For example
maybe you guys can help me i have a little problem here: I have
I'm learning Ruby right now and I got stuck, maybe you guys can help
so I've run into a problem recently, and maybe you guys can help. So
maybe you can help me out here. I have an issue with my codeigniter
maybe you guys can help me with this. I am trying to implement reCAPTCHA
I just can't figure out how is this happening, maybe you guys can help
I have a website where people can upload 30+mb of data in a single
I need a little help writing a query. I have this data... vDir iNumber
I have a quick question i hope you guys can answer, i've got a

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.