【问题标题】:regex + replace character between tags using Notepad++正则表达式 + 使用 Notepad++ 替换标签之间的字符
【发布时间】:2014-05-06 01:46:42
【问题描述】:

我认为使用正则表达式查找/替换是我最好的选择。但如果有其他建议/建议,我将概述我正在尝试做的事情

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

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

  3. 我目前正在努力将平面 .xml 文件中的数据“积压”数据导入数据库。

    一个。我曾尝试使用SQL LOAD XML INFILE:https://stackoverflow.com/questions/22775206/how-to-use-load-xml-infile-with-special-characters,但不知道如何解析/转义特殊字符数据...

    b.我现在已经转到PHP/SimpleXML,但是我又发现了 XML 中某些节点/元素中的特殊字符的麻烦。 (可以是单引号或双引号,'&' 符号,不确定.. 它是一个 'description' 字段)

当我尝试加载 XML 文件时.. 出现错误:

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

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

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

但也许有更好的方法来解析 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 搜索介于两个 &lt; tags &gt; &lt; /tags &gt; 之间的特殊字符(单引号/撇号)

这是我为搜索部分尝试过的正则表达式..(我似乎无法确定替换部分,它出于某种原因用撇号替换了整个单词?)

搜索:(记事本++)

[?=<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>

谢谢

【问题讨论】:

  • 听起来你的 XML 是无效的。你不应该解决这个问题吗?简单的撇号没有任何问题。你能发布一个 XML 文件的例子吗?
  • 我同意 Phil 关于损坏的 xml 并需要一个示例的观点,如您所见 here 特定字符需要转义。您是否能够后退一步并修复生成 xml 文件的任何内容?
  • 感谢您的回复。是的,这就是我想要做的,在用 PHP/SimpleXML 解析 XML 之前“修复”它。 (因此用于替换单引号/撇号的正则表达式问题)如上所述,这是一个 FLAT .xml 文件。在任何编辑器中打开它添加一个新的节点/元素保存)..如果我转义或替换它们都没关系..(我无法让正则表达式工作)我不确定为什么这会有所帮助......但是在这里是 XML 布局的 sn-p:
  • xml 示例添加到上面的原始帖子(评论太长)
  • 我的正则表达式尝试只产生失败的结果:can't become ca' []'[] '以及其他几次搜索尝试(尽管替换似乎是失败的)

标签: php regex replace simplexml


【解决方案1】:

使用 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>

【讨论】:

    猜你喜欢
    • 2013-07-27
    • 2015-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多