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

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 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择
  • ¥15 部分网页页面无法显示!
  • ¥15 怎样解决power bi 中设置管理聚合,详细信息表和详细信息列显示灰色,而不能选择相应的内容呢?
  • ¥15 QTOF MSE数据分析
  • ¥15 平板录音机录音问题解决