正确地替换包含';a';使用PHP的元素
我目前正在写一个类,它获取Facebook和Twitter的提要,然后将它们合并成一个,在网站上显示 然而,我遇到了一个问题,即限制输入文本的输出,因为正确地替换包含';a';使用PHP的元素,php,html,substr,Php,Html,Substr,我目前正在写一个类,它获取Facebook和Twitter的提要,然后将它们合并成一个,在网站上显示 然而,我遇到了一个问题,即限制输入文本的输出,因为a元素在一个简单的substr函数后不会被关闭 假设我有这个字符串: 'Check out our site at <a href="http://site.com/">site.com</a>' 一个未关闭的元素,它将把我网站的其余部分变成一个链接 我想也许使用DOMDocument我可以暂时用之间的部分替换完整的ur
a
元素在一个简单的substr
函数后不会被关闭
假设我有这个字符串:
'Check out our site at <a href="http://site.com/">site.com</a>'
一个未关闭的元素,它将把我网站的其余部分变成一个链接
我想也许使用DOMDocument我可以暂时用
之间的部分替换完整的url,做减法,然后重新应用链接
然而,我不知道如何做到这一点,这给我留下了另一个问题/选择:如果-考虑到我能够临时替换链接-在减法后,我最终得到一半的链接,该怎么办:
'Check out our site at sit'
然后很难重新应用链接,因此最好用[[id]]
之类的内容替换它,让脚本记住文本的长度
不管怎样,有没有人能帮我解决这个问题
EDIT它只适用于a
标记,因为我在其他所有东西上都strip\u标记。这段代码非常适用于此
例如:
echo substrws("Check out our site at <a href=\"http://site.com/\">site.com</a>. It's really <strong>nice</strong>", 50);
echo substrws(“查看我们的网站。它真的不错””,50);
收益率:
查看我们的网站。
代码:
/**
*具有html标记感知的单词敏感子字符串函数
*@param text要剪切的文本
*@param len切割字符串的最大长度
*@返回字符串
**/
函数substrws($text,$len=180){
如果((strlen($text)>$len)){
$whitespaceposition=strpos($text,“,$len)-1;
如果($whitespaceposition>0)
$text=substr($text,0,($whitespaceposition+1));
//关闭未关闭的html标记
if(preg_match_all(“| |,$text,$aBuffer)){
如果(!empty($aBuffer[1])){
preg_match_all(“| |,$text,$aBuffer2);
if(count($aBuffer[1])!=count($aBuffer[1])){
foreach($aBuffer[1]作为$index=>$tag){
if(空($aBuffer2[1][$index])| |$aBuffer2[1][$index]!=$tag)
$text.='';
}
}
}
}
}
返回$text;
}
另一个解决方案是php的strip_tags()函数,如下所示:
<?php
$text = '<p>Check out our site at </p><!-- other html stuff anywhere--> <a href="http://site.com/">site.com</a>';
echo strip_tags($text);
echo "\n";
// juts allow <p> and <a>
echo strip_tags($text, '<p><a>');
?>
最后我编写了自己的函数,可能需要一些改进,但它可以工作:
private function substr_html($input,$limit){
$original = $input;
if(strlen($input) <= $limit)
return $input;
$pattern = '#<a\s+.*?href=[\'"]([^\'"]+)[\'"]\s*?.*?>((?:(?!</a>).)*)</a>#i';
// Match all 'a' elements
preg_match_all($pattern,$input,$matches);
// If no links were found, perform a simple substr()
if(count($matches[0]) == 0)
return substr($input,0,$limit).'...';
$uni = sha1(uniqid());
preg_replace($pattern,$uni,$input);
$input = explode($uni,$input);
$tmp = $output = '';
// Go through the splitted input
foreach($input as $i){
if(strlen($tmp.$i) < $limit){
// If we can fit the next text value without reaching the limit, do it
$tmp .= $i;
$output .= $i;
}else{
// Add whatever we can fit from the last text value and break the loop
$diff = abs($limit - strlen($tmp));
$output .= substr($i,0,$diff);
break;
}
if(strlen($tmp) < $limit){ // Do we still have room before we reach the limit?
$nextlink = array_shift($matches[1]);
$nexttext = array_shift($matches[2]);
if(strip_tags($nexttext,$this->allowed_tags) != '')
if(strlen($tmp.$nexttext) < $limit){
// Add the next link if it fits
$tmp .= $nexttext;
$output .= '<a href="'.$nextlink.'" target="_blank">'.$nexttext.'</a>';
}else{
// Add whatever we can fit from the last link and break the loop
$diff = abs($limit - strlen($tmp));
$output .= '<a href="'.$nextlink.'" target="_blank">'.substr($nexttext,0,$diff).'</a>';
break;
}
}
}
// Trim string and remove linebreaks
$output = trim(preg_replace('/((<br>|<br\/>|<br \/>){1,})/'," ",$output));
return $output.(strip_tags($original) != strip_tags($output) ? '...' : '');
}
private function substr\u html($input,$limit){
$original=$input;
如果(strlen($input)允许_标记)!=“”)
如果(strlen($tmp.$nexttext)<$limit){
//如果合适,添加下一个链接
$tmp.=$nexttext;
$output.='';
}否则{
//从最后一个链接中添加我们能适应的内容,打破循环
$diff=abs($limit-strlen($tmp));
$output.='';
打破
}
}
}
//修剪字符串并删除换行符
$output=trim(preg|u replace('/(
|
){1,})/','',$output));
返回$output.(strip_标签($original)!=strip_标签($output)?“…”:”;
}
请参见InnerText:如果字符串类似于“在\>site.com上查看我们的网站”。它真的很好,输出变成了“在site.com/”>site.com上查看我们的站点”,经过更多的测试,它实际上开始在各个级别上运行。所以我尝试了更多的东西,并最终编写了自己的代码(可能会有所改进,但这是完美的)。
<?php
$text = '<p>Check out our site at </p><!-- other html stuff anywhere--> <a href="http://site.com/">site.com</a>';
echo strip_tags($text);
echo "\n";
// juts allow <p> and <a>
echo strip_tags($text, '<p><a>');
?>
private function substr_html($input,$limit){
$original = $input;
if(strlen($input) <= $limit)
return $input;
$pattern = '#<a\s+.*?href=[\'"]([^\'"]+)[\'"]\s*?.*?>((?:(?!</a>).)*)</a>#i';
// Match all 'a' elements
preg_match_all($pattern,$input,$matches);
// If no links were found, perform a simple substr()
if(count($matches[0]) == 0)
return substr($input,0,$limit).'...';
$uni = sha1(uniqid());
preg_replace($pattern,$uni,$input);
$input = explode($uni,$input);
$tmp = $output = '';
// Go through the splitted input
foreach($input as $i){
if(strlen($tmp.$i) < $limit){
// If we can fit the next text value without reaching the limit, do it
$tmp .= $i;
$output .= $i;
}else{
// Add whatever we can fit from the last text value and break the loop
$diff = abs($limit - strlen($tmp));
$output .= substr($i,0,$diff);
break;
}
if(strlen($tmp) < $limit){ // Do we still have room before we reach the limit?
$nextlink = array_shift($matches[1]);
$nexttext = array_shift($matches[2]);
if(strip_tags($nexttext,$this->allowed_tags) != '')
if(strlen($tmp.$nexttext) < $limit){
// Add the next link if it fits
$tmp .= $nexttext;
$output .= '<a href="'.$nextlink.'" target="_blank">'.$nexttext.'</a>';
}else{
// Add whatever we can fit from the last link and break the loop
$diff = abs($limit - strlen($tmp));
$output .= '<a href="'.$nextlink.'" target="_blank">'.substr($nexttext,0,$diff).'</a>';
break;
}
}
}
// Trim string and remove linebreaks
$output = trim(preg_replace('/((<br>|<br\/>|<br \/>){1,})/'," ",$output));
return $output.(strip_tags($original) != strip_tags($output) ? '...' : '');
}