将所有代码放在一个函数中?

时间:2015-05-05 01:57:27

标签: php text

这是原作

echo '<div style="float: right; width:31%; text-align: center; ">
      <img src="/img/1.png" width="12" height="12"/><em> '.
      $results['tagline']
      .' </em></span><img title="'.
      $results['tagline']
      .' "src="/img/2.png" width="12" height="12" "> </div><br>';

所以.$results['tagline'].在哪里,我想用下面的代码替换

$string = ' '.$results['tagline'].' '; 

$maxLength = 25;

if (strlen($string) > $maxLength) {
    $stringCut = substr($string, 0, $maxLength);
    $string = substr($stringCut, 0, strrpos($stringCut, ' ')); 
}
echo $string; 
echo '<span style=""><span title2="'.$results['tagline'].'" class="masterTooltip">...</span></span>';

或者是否可以正确地纠正所有文本以使代码按照我想要的方式工作!

1 个答案:

答案 0 :(得分:1)

哦,在这里,我应该指出html现在无效吗?

function title($t){
$string = ' '.$t.' '; 

$maxLength = 25;

if (strlen($string) > $maxLength) {
    $stringCut = substr($string, 0, $maxLength);
    $string = substr($stringCut, 0, strrpos($stringCut, ' ')); 
}
$out= $string; 
$out.= '<span style=""><span title2="'.$t.'" class="masterTooltip">...</span></span>';

return $out;
}

echo '<div style="float: right; width:31%; text-align: center; "><img src="/img/1.png" width="12" height="12" " /><em> '.title($results['tagline']).' </em></span><img title="'.$results['tagline'].'" src="/img/2.png" width="12" height="12" "> </div><br>';