php输入框限制输入字符,PHP限制文本字符串不包括html标签?

截断HTML代码中单词的函数:

//+ Jonas Raoni Soares Silva

//@ http://jsfromhell.com

function truncate($text, $length, $suffix = '…', $isHTML = true) {

$i = 0;

$simpleTags=array('br'=>true,'hr'=>true,'input'=>true,'image'=>true,'link'=>true,'meta'=>true);

$tags = array();

if($isHTML){

preg_match_all('/]+>([^

foreach($m as $o){

if($o[0][1] - $i >= $length)

break;

$t = substr(strtok($o[0][0], " \t\n\r\0\x0B>"), 1);

// test if the tag is unpaired, then we mustn't save them

if($t[0] != '/' && (!isset($simpleTags[$t])))

$tags[] = $t;

elseif(end($tags) == substr($t, 1))

array_pop($tags);

$i += $o[1][1] - $o[0][1];

}

}

// output without closing tags

$output = substr($text, 0, $length = min(strlen($text), $length + $i));

// closing tags

$output2 = (count($tags = array_reverse($tags)) ? '' . implode('>', $tags) . '>' : '');

// Find last space or HTML tag (solving problem with last space in HTML tag eg. )

$pos = (int)end(end(preg_split('/<.>| /', $output, -1, PREG_SPLIT_OFFSET_CAPTURE)));

// Append closing tags to output

$output.=$output2;

// Get everything until last space

$one = substr($output, 0, $pos);

// Get the rest

$two = substr($output, $pos, (strlen($output) - $pos));

// Extract all tags from the last bit

preg_match_all('//s', $two, $tags);

// Add suffix if needed

if (strlen($text) > $length) { $one .= $suffix; }

// Re-attach tags

$output = $one . implode($tags[0]);

//added to remove unnecessary closure

$output = str_replace('!-->','',$output);

return $output;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值