dongyinju5977 2017-07-07 16:28
浏览 25
已采纳

删除字符串中的第一个html标记

i have this text

<?php
   $example1 = '<b class="counter">1</b> This is a computer';
   $example2 = '<b class="counter">5</b> i have a laptop';
   $example3 = '<b class="counter">1</b> i need a smartphone';
   $example4 = 'i need a car'; // does not have <b> tag
?>

i'm looking for a php function to remove a part of the string, starting from <b> to </b>, so the results should be like :

$example1_result = 'This is a computer';
$example2_result = 'i have a laptop';
$example3_result = 'i need a smartphone';
$example4_result = 'i need a car';
  • 写回答

4条回答 默认 最新

  • dongzhaiqiang6108 2017-07-07 16:47
    关注

    Make sure to escape from your "counter" otherwise it will not work, but the others have already answered your question with a better version using regular expressions.

    <?php
    $example1 = "<b class=\"counter\">1</b> This is a computer";
    $example2 = "<b class=\"counter\">5</b> i have a laptop";
    $example3 = "<b class=\"counter\">1</b> i need a smartphone";
    
    echo strip_tags($example1); //Outputs: 1 This is a computer
    
    $example1 = trim(substr(strstr($example1, '</b>'), strlen('</b>')));
    echo $example1; //Outputs: This is a computer
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用