Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP - shorten text without touching links (UTF-8)
    primarykey
    data
    text
    <p>I need to shorten a given text (with different encodings!) - eg. to 140 characters - without touching the links. </p> <p><strong>Example:</strong></p> <pre><code>Lorem ipsum dolor sit amet: http://bit.ly/111111 Consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat. http://bit.ly/222222 Sed diam voluptua. At vero eos et accusam et justo duo dolores. http://bit.ly/111111 </code></pre> <p>Should end up as:</p> <pre><code>Lorem ipsum dolor sit amet: http://bit.ly/111111 Consetetur sadipscing elitr, sed diam nonumy... http://bit.ly/222222 http://bit.ly/111111 </code></pre> <p>My actual code with examples is here: <a href="http://phpfiddle.org/lite/code/er7-sty" rel="nofollow noreferrer">http://phpfiddle.org/lite/code/er7-sty</a></p> <pre><code>function shortenMessage($message,$limit=140,$encoding='utf-8') { if (mb_strlen($message,$encoding) &lt;= $limit) return $message; echo '&lt;pre&gt;&lt;h3&gt;Original message:&lt;br /&gt;'.$message.'&lt;hr&gt;'; # search positions of links $reg_exUrl = "/(http|https)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/"; preg_match_all ($reg_exUrl, $message, $links,PREG_OFFSET_CAPTURE); echo 'Links found:&lt;br /&gt;'; var_dump($links[0]); echo '&lt;hr&gt;'; $position = array(); $len = 0; # search utf-8 position of links foreach ($links[0] as $values) { $url = $values[0]; $offset = $values[1]; #$pos = mb_strpos($message, $url, $offset, $encoding); # doesnt work $pos = mb_strpos($message, $url, 0, $encoding); $position[$pos] = $url; # delete url from string $message = str_replace($url, '', $message); $len += mb_strlen($url,$encoding); # sum lenght of urls to cut from maxlenght } echo 'UTF-8 Positions:&lt;br /&gt;'; var_dump($position); echo '&lt;hr&gt;'; # shorten text $maxlenght = $limit - $len - 7; # 7 is a security buffer while ($maxlenght &lt; 0) { # too many urls? then cut some... array_shift($position); $len -= mb_strlen($position[0],$encoding); $maxlenght = $limit - $len - 6; } echo 'UTF-8 Positions shortened:&lt;br /&gt;'; var_dump($position); echo '&lt;hr&gt;'; $message = mb_substr($message,0,$maxlenght,$encoding).'... '; echo 'Shortened message without urls:&lt;br /&gt;'; var_dump($message); echo '&lt;hr&gt;'; # re-insert urls at right positions $addpos = 0; foreach ($position as $pos =&gt; $url) { $pos += $addpos; if ($pos &lt; mb_strlen($message,$encoding)) { $message = mb_substr($message,0,$pos,$encoding).$url.mb_substr($message,$pos,mb_strlen($message),$encoding); } else { $message .= ' '.$url; } $addpos += mb_strlen($url,$encoding); } echo 'Shortened message:&lt;br /&gt;'; var_dump($message); echo '&lt;hr&gt;'; return $message; } </code></pre> <p>It works, when there are only different links in the text, but fails, when one link is duplicate. </p> <p>I've already tried to take the position from preg_match_all as offset for the mb_strpos, but I thinks this fails, because of the preg-match-utf8-problem. </p> <p>I've seen <a href="https://stackoverflow.com/questions/9042975/shortening-text-tweet-like-without-cutting-links-inside">Shortening text tweet-like without cutting links inside</a> already, but they didn't take care of the encoding and deal with html tags...</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload