您可以使用以下函数来自动换行(utf8-safe!)您的文本并创建链接。 wrap() 将您的文本拆分为数组字符串。这比指定单词的数量更有意义,因为单词可以是很短的“hi”或很长的“hippopotomonstrosesquipedaliophobia”。不要忘记转义输出。
示例:echo wrappedlink(htmlspecialchars($row['email']), 20);
function wrappedlink($str, $len) {
return '<script language="Javascript">function toggleDisplay(id) { document.getElementById(id).style.display = (document.getElementById(id).style.display == "block") ? "none" : "block"; }</script>'."\n".
'<a href="javascript:toggleDisplay(\''.($id=substr(md5(rand().$str),0,8)).'\');">'.wrap($str, $len)[0].'</a> <div id="'.$id.'" style="display:none;">'.$str.'</div>';
}
function wrap($string, $width) {
if (($len=mb_strlen($string, 'UTF-8')) <= $width) return array(
$string
);
$return=array();
$last_space=FALSE;
$i=0;
do {
if (mb_substr($string, $i, 1, 'UTF-8') == ' ') $last_space=$i;
if ($i > $width) {
$last_space=($last_space == 0)?$width:$last_space;
$return[]=trim(mb_substr($string, 0, $last_space, 'UTF-8'));
$string=mb_substr($string, $last_space, $len, 'UTF-8');
$len=mb_strlen($string, 'UTF-8');
$i=0;
}
$i++;
} while ($i < $len);
$return[]=trim($string);
return $return;
}