dongping2023 2013-04-10 04:10
浏览 195
已采纳

PHP从字符串中删除域名[重复]

This question already has an answer here:

I need to remove the domain name from the end of a string. I have tried the following code:

    $domainNAME="example.com";
    $result="_xmpp-client._tcp.example.com..example.com"
    $string = $result;
    $string = str_replace(".".$domainNAME, " ", $string);

Here the result is "_xmpp-client._tcp.". But the result should be "_xmpp-client._tcp.example.com.".

I need to remove the domain name only from the end of string, if domain name exists anywhere in the string it should not be removed.How can I change the code for that?

Any help should be appreciated!

</div>
  • 写回答

6条回答 默认 最新

  • dsjbest2015 2013-04-10 04:19
    关注

    You could use preg_replace and specify the end of string marker $:

    $string = preg_replace("/" . $domainNAME . "$/", " ", $string);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制