php function like wordpress the_content()


php function like wordpress the_content()

aphp中是否有一个函数(如Wordpress的the_content()))可以限制文本字符串而不会破坏其中可能存在的html标签?

//example:
echo substr ('<p>this <a>is</a> a test</p>', 0, 10);
//output: "<p>this <a"
//desired output: "<p>this </p>"

不,没有。 但是你可以深入研究Wordpress的来源,看看the_content()做了什么来实现这一目标。 我自己没有这样做。 在较短的版本中保留HTML标记将是一个棘手的代码块。

你需要这个做什么? 你只是想获取较长的 HTML blob 的预览吗? 过去,我通过使用strip_tags()substr()的组合,并将剥离的内容部分包装在新的<p>标签中。