Modify a php limit text function adding some kind of offset to it
<p>Maybe you guys can help:</p>

<p>I have a variable called $bio with bio data.</p>

<pre><code>$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";
</code></pre>

<p>I search the $bio using a set of functions to search for a certain word, lets say <strong>"author"</strong> which <strong>adds a span class around that word</strong>, and I get:</p>

<pre><code>$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 &lt;span class=\"highlight\"&gt;author&lt;/span&gt; of the question";
</code></pre>

<p>I use a function to limit the text to 85 chars:</p>

<pre><code>$bio = limit_text($bio,85);
</code></pre>

<p><strong>The problem</strong> is when there are more then 80 chars before the word <strong>"author"</strong> in <strong>$bio</strong>.
When the <code>limit_text()</code> is applied, I won't see the highlighted word author.</p>

<p><strong>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:</strong></p>

<pre><code>*"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 "*
</code></pre>

<p>Hope you understood what I mean, if not, please comment and I'll try to explain better.</p>

<p>Here is my <code>limit_text()</code> function:</p>

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

<p><strong>UPDATE</strong>:</p>

<pre><code>$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);
</code></pre>

<p>The <code>customHighlights</code> function which adds the span class highlighted:</p>

<pre><code>function addRegEx($word){ // Highlight Words
        return "/" . $word . '[^ ,\,,.,?,\.]*/i';
    }
    function highlight($word){
        return "&lt;span class='highlighted'&gt;".$word[0]."&lt;/span&gt;";
    }
    function customHighlights($searchString,$toHighlight){
        $searchFor = array_map('addRegEx',$toHighlight);
        $result = preg_replace_callback($searchFor,'highlight',$searchString);
        return $result;
    }
</code></pre>
