douxingti9307 2015-10-13 08:03
浏览 19
已采纳

删除特定符号前后的字符

I have a string <?php $linktitle = get_the_title();?> that stores the title of the post. The string has a title like this:

If Your Wi-Fi Is Terrible, Check Your Router – The New York Times

How can I remove everything before and including the ? I'm using: <?php echo strstr($linktitle, '&#8211;'); ?> and it outputs:

– The New York Times

  • 写回答

2条回答 默认 最新

  • duan02143 2015-10-13 08:10
    关注

    Try using preg_replace:

    preg_replace("/.+?( –)/", '', $linktitle)
    

    If you want to remove the whitespace after the - too:

    preg_replace("/.+?( –)\s*/", '', $linktitle)
    

    This uses regular expression to match a pattern defined by any character except newline 1 or more times (.+), until it meets (?) a space followed by a dash (( –)), then a whitespace (\s) 0 or more times (*). Preg_replace then replaces the matched pattern with an empty string.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)