douyi9597 2018-06-28 00:28
浏览 108

如何在php中使用正则表达式搜索XML文件

I am curious if I can search an XML file for a certain tag with regular expressions. I can search the file if I use fopen('foo.xml'); but it will only allow me to search the content between the tags not the tags them self. My objective for this is I hope to create a function that will allow me to delete all the content between two tags for example between users which are in a xml file. He language that I am using is PHP.

Thanks in advance john.

  • 写回答

3条回答 默认 最新

  • dsfds656545 2018-06-28 00:41
    关注

    You should use something like SimpleXMLto handle/edit XML files.

    If you really insist on doing it by treating the SML file as a string you can do something like this (or you can use regex). But you should use an XML library.

    // get your file as a string 
    $yourXML = file_get_contents($file) ;
    
    $posStart = stripos($yourXML,'<users>') + strlen('<users>') ;
    $posEnd = stripos($yourXML,'</users>')  ;
    
    $newXML = substr($yourXML,0,$posStart) . substr($yourXML,$posEnd) ;
    
    // <users> is now empty
    echo $newXML ;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题