简体   繁体   English

PHP substr防止由于html标签引起的链接

[英]PHP substr prevents link due to html tag

I have a blog website which lists all of the latest blogs on the homepage with a photo, limited text and then a text link to the full blog post. 我有一个博客网站,该网站列出了首页上的所有最新博客,包括照片,有限的文字,然后是指向完整博客文章的文本链接。 The problem is that if I have an tag within the blog text, it gets in the way of the blog link. 问题是,如果我在博客文本中有一个标签,它将妨碍博客链接。

What I mean is: 我的意思是:

If this is my blog post - " Hello, I am wayne, writer of <a href="website.com">Bloggers Weekly</a> and this is my first article " 如果这是我的博客文章-“ Hello, I am wayne, writer of <a href="website.com">Bloggers Weekly</a> and this is my first article

And I want to abbreviate this to display 40 characters, or "Hello, I am wayne, writer of Bloggers W ... Read More" 我想将其缩写为显示40个字符,或者“您好,我是韦恩,Bloggers W ...的作家...阅读更多”

The Read More link doesn't show up properly because in the original html there is already an <a> tag which prevents the second link from working. Read More链接无法正确显示,因为在原始html中已经有一个<a>标记,该标记会阻止第二个链接正常工作。 Is there any way around this? 有没有办法解决?

您可以使用strip_tags从字符串中删除HTML标签。

在回显博客文章之前,可以使用strip_tags删除HTML标记。

echo substr(strip_tags($blog_post),0,40);

you can check for the opening less than symbol before shortening the string, or lengthen the string if you want to keep the link. 您可以在缩短字符串之前检查开头是否小于符号,或者如果要保留链接,则可以延长字符串。

$shortString = ($openPos = strpos($shortString, '<a')) !== false ? substr($shortString, 0 , ($openPos - 2) : substr($yourString,0,40);

This will cut it short just before the opening 这将在开幕前缩短它的时间

otherwise if you want to keep the link you can use the same method and extend the short description 否则,如果您想保留链接,则可以使用相同的方法并扩展简短说明

$shortString = ($closePos = strpos($yourString, '</a>')) !== false ? substr($yourString, 0 , ($closePos + 3) : substr($yourString,0,40);

I haven't tested this, but it should leave the link intact. 我还没有测试过,但是它应该保持链接完整。 Note the these will make the string less than 40 characters if the closing/opening 'a' tag comes before the first 40 characters. 请注意,如果关闭/打开“ a”标签位于前40个字符之前,则这些字符串将使字符串少于40个字符。

The benefit to this over strip tags is that you wont leave something that should be a link in the text if you don't want it to be a link. 相对于条形标签,这样做的好处是,如果您不希望将其作为链接,则不会在文本中留下应作为链接的内容。 ie.. 'Click here' text that is not clickable. 即..不可单击的“单击此处”文本。

Try this , 尝试这个 ,

    $string = "Hello, I am wayne, writer of Bloggers Weekly and this is my first article";

           if (strlen($string) > 40) 
            {

                // truncate string
                $stringCut  = substr($string, 0, 40);
                $read_more = 'read more..'."<a href='website.com'>";                    
                $string     = substr($stringCut, 0, strrpos($stringCut, ' ')).$read_more;
            }

echo $string;

Read More link is not working properly because when you use substr, substr also inlcudes partial anchor tag. Read More链接无法正常工作,因为当您使用substr时,substr也包括部分锚标记。 So better way is first remove tags and then apply anchor tag to Read More only. 因此,更好的方法是先删除标签,然后将锚标签应用于“只读”。

<?
echo substr(strip_tags($blog),0,40) . "<a href='#link'>Read More </a>";

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM