Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP Substr函数修剪问题_Php_Substr_Html Encode - Fatal编程技术网

PHP Substr函数修剪问题

PHP Substr函数修剪问题,php,substr,html-encode,Php,Substr,Html Encode,我正在使用以下代码修剪标题并在我的屏幕上显示: 但是HTML字符实体存在问题。当我使用substr函数修剪标题时,substr函数也修剪HTML字符实体 所以我试着使用这个函数,但我做得不是很好 有人能帮我吗?我想你可以使用那里的功能,例如: substr(strip_tags($title), 0, 20); 这将只处理不包括任何html字符的标题。尝试以下操作: $output = htmlentities(substr(html_entity_decode($input), 0, 2

我正在使用以下代码修剪标题并在我的屏幕上显示:


但是HTML字符实体存在问题。当我使用
substr
函数修剪标题时,
substr
函数也修剪HTML字符实体

所以我试着使用这个函数,但我做得不是很好

有人能帮我吗?

我想你可以使用那里的功能,例如:

substr(strip_tags($title), 0, 20);
这将只处理不包括任何html字符的标题。

尝试以下操作:

$output = htmlentities(substr(html_entity_decode($input), 0, 20));
这将解码所有实体,因此
substr
不会破坏任何内容。之后,您可以将所有字符编码回其实体。

使用此功能

<?php
  function keephtml($string){
          $res = htmlentities($string);
          $res = str_replace("&lt;","<",$res);
          $res = str_replace("&gt;",">",$res);
          $res = str_replace("&quot;",'"',$res);
          $res = str_replace("&amp;",'&',$res);
          return $res;
}
?>

如果您可以将html编码保留到最后一分钟(例如,当您实际回显$title字符串时),则会更干净。当然,这意味着您必须记住自己对所有字符串进行编码。

不,它不起作用。我认为
strip\u标签
功能是剥离HTML标签。但我想剥离HTML字符实体()。
<?php
  function keephtml($string){
          $res = htmlentities($string);
          $res = str_replace("&lt;","<",$res);
          $res = str_replace("&gt;",">",$res);
          $res = str_replace("&quot;",'"',$res);
          $res = str_replace("&amp;",'&',$res);
          return $res;
}
?>