PHP的 rtrim() 函数,去除字符串右侧指定字符串的使用疑问
<?php $str1 = '<div class="swiper-slide"><div> <img src="uploads/image/20210831/1630375137.jpg" alt=""> </div></div><div class="swiper-slide">'; var_dump(htmlentities($str1)); var_dump('<br>'); var_dump('<br>'); $str2 = rtrim($str1,'<div class="swiper-slide">'); var_dump(htmlentities($str2));die;
按理来说,去除掉右边的 “ <div class="swiper-slide"> ”的字符串后,结果应该是:
<div class="swiper-slide"><div> <img src="uploads/image/20210831/1630375137.jpg" alt=""> </div></div>
但是我实际运行的结果却是:
但是我发现,如果 $str1 的内容是有换行格式的,运行结果就符合预期
<?php $str1 = '<div class="swiper-slide"> <div> <img src="uploads/image/20210831/1630375137.jpg" alt=""> </div> </div> <div class="swiper-slide">'; var_dump(htmlentities($str1)); var_dump('<br>'); var_dump('<br>'); $str2 = rtrim($str1,'<div class="swiper-slide">'); var_dump(htmlentities($str2));die;
运行结果如下:
有没有大哥知道这是怎么回事?