正则表达式+使用Notepad ++替换标签之间的字符

时间:2014-05-06 01:46:43

标签: php regex replace simplexml

我认为使用正则表达式来查找/替换是我最好的选择...但是我会概述我正在尝试做什么,以防有其他建议/建议

  1. 我有一个FLAT(静态).xml文件

  2. 我正在转换使用数据库而不是加载这个平面.xml文件,(这将是您通常的表单接口/ GUI,使用PHP / PDO提交到MySQL数据库(这里没有SQL注入)哥们!);)(这已经很好了)

  3. 我目前正在努力将平面.xml文件中的数据“回溯”记录到数据库中。

    一个。我曾尝试使用SQL LOAD XML INFILEhttps://stackoverflow.com/questions/22775206/how-to-use-load-xml-infile-with-special-characters,但无法弄清楚如何解析/转义特殊字符数据......

    湾我现在已经转移到PHP/SimpleXML,但是我在XML中的某些节点/元素中使用特殊字符再次发现问题。 (可以是单引号或双引号,'&'符号,不确定..它是'描述'字段)

  4. 当我尝试加载XML文件时......我收到错误:

      

    警告:simplexml_load_file()[function.simplexml-load-file]:xml_source.xml:142:解析器错误:打开和结束标记不匹配:BR行142和C:\ wamp \ www \ xml_tests \ simpleXML_test中的描述。第4行的php

    如果我找到xml节点,并用'替换撇号,它将解析并移动到具有破坏它的特殊字符的下一个节点。

    我的直觉是尝试找出如何使用REGEX在两个标签之间搜索任何撇号(或任何特殊字符)....并在数据输入数据库之前进行替换。< / p>

    但也许有更好的方法来解析PHP / SimpleXML ..然而,似乎我需要在SimpleXML甚至读取文件之前摆脱它?

    if(!$xml=simplexml_load_file('xml_source.xml')){
        trigger_error('Error reading XML file', E_USER_ERROR);
    }
    
    foreach($xml->entry as $entry){
        echo 'Name: ' . $entry->name . '<br />';
        echo 'Date: ' . $entry->attributes()->date_entered . '<br />';
    }
    

    简单的测试,但如上所述,我得到上面的错误,撇号仍在那里。

    如何使用REGEX搜索两个< tags > < /tags >

    之间的特殊字符(单引号/撇号)

    这是我为SEARCH部分尝试过的REGEX ..(我似乎无法确定替换部分是否因为某些原因用撇号取代了整个单词?)

    搜索:(记事本++)

    [?=<description>].'[?=</description>]
    

    替换:

    \&apos;
    

    XML的例子:

    <?xml version="1.0" encoding="UTF-8"?>
    <entries>
        <entry submissionDate="2013-02-18">
            <fontName>String/Text</fontName>    
            <fontCreator>String/Text</fontCreator>
            <fontFormat>String/Text</fontFormat>
            <optimized>String/Text</optimized>
            <fontPrice>Nuumber/Int (with decimal)</fontPrice>
            <fontImage>String/Text</fontImage>
            <fontURL>Int</fontURL>
            <description>Don't can't lot's of 'single' quote/apostrophes and "double quotes" too</description>
            <piracyVid>String/Text</piracyVid>
            <demoLink>String/Text</demoLink>
        </entry>
    
        <entry submissionDate="2013-02-18">
            <fontName>String/Text</fontName>    
            <fontCreator>String/Text</fontCreator>
            <fontFormat>String/Text</fontFormat>
            <optimized>String/Text</optimized>
            <fontPrice>Nuumber/Int (with decimal)</fontPrice>
            <fontImage>String/Text</fontImage>
            <fontURL>Int</fontURL>
            <description>Don't can't lot's of 'single' quote/apostrophes and "double quotes" too</description>
            <piracyVid>String/Text</piracyVid>
            <demoLink>String/Text</demoLink>
        </entry>
    </entries>
    

    由于

1 个答案:

答案 0 :(得分:0)

使用SimpleXML,它很简单:

foreach($xml->xpath('//entry/description') as $node) {
    $node[0] = preg_replace('/"/u', '(say it sam: \0)', $node);
}

$xml->asXML('php://output');

以你的例子给出:

<?xml version="1.0" encoding="UTF-8"?>
<entries>
    <entry submissionDate="2013-02-18">
        <fontName>String/Text</fontName>
        <fontCreator>String/Text</fontCreator>
        <fontFormat>String/Text</fontFormat>
        <optimized>String/Text</optimized>
        <fontPrice>Nuumber/Int (with decimal)</fontPrice>
        <fontImage>String/Text</fontImage>
        <fontURL>Int</fontURL>
        <description>Don't can't lot's of 'single' quote/apostrophes and (say it sam: ")double quotes(say it sam: ") too</description>
        <piracyVid>String/Text</piracyVid>
        <demoLink>String/Text</demoLink>
    </entry>

    <entry submissionDate="2013-02-18">
        <fontName>String/Text</fontName>
        <fontCreator>String/Text</fontCreator>
        <fontFormat>String/Text</fontFormat>
        <optimized>String/Text</optimized>
        <fontPrice>Nuumber/Int (with decimal)</fontPrice>
        <fontImage>String/Text</fontImage>
        <fontURL>Int</fontURL>
        <description>Don't can't lot's of 'single' quote/apostrophes and (say it sam: ")double quotes(say it sam: ") too</description>
        <piracyVid>String/Text</piracyVid>
        <demoLink>String/Text</demoLink>
    </entry>
</entries>