Php 正则表达式+使用记事本替换标记之间的字符++
我认为使用正则表达式查找/替换是我最好的选择。。但我会概述一下,如果有其他建议,我会尝试做些什么 我有一个扁平的static.xml文件 我正在将事情转换为使用数据库,而不是加载这个flat.xml文件,这将是您常用的表单界面/GUI,它使用PHP/PDO提交到MySQL数据库,这里没有SQL注入;这已经很好了 我目前正致力于从flat.xml文件中获取数据的“备份日志”到数据库中 a。我曾尝试使用SQLLOADXMLINFILE:但不知道如何解析/转义特殊字符数据 b。我现在已经转到PHP/SimpleXML,但是在XML中的一些节点/元素中使用特殊字符时,我再次发现了问题。可以是单引号或双引号,&,不确定。。这是一个“描述”字段 当我尝试加载XML文件时。。我得到一个错误: 警告:simplexml\u加载文件[函数.simplexml加载文件]:xml\u源。xml:142:解析器错误:开始和结束标记不匹配:BR第142行和第4行C:\wamp\www\xml\u tests\simplexml\u test.php中的说明 如果我找到xml节点,并将撇号替换为'它将解析并移动到下一个节点,该节点有一个特殊字符将其打断 我的直觉是尝试找出如何使用正则表达式在两个标记之间搜索任何撇号或任何特殊字符…并在数据输入数据库之前进行替换 但也许有更好的方法通过PHP/SimpleXML进行解析。。然而,在SimpleXML读取文件之前,我似乎需要消除这个问题Php 正则表达式+使用记事本替换标记之间的字符++,php,regex,replace,simplexml,Php,Regex,Replace,Simplexml,我认为使用正则表达式查找/替换是我最好的选择。。但我会概述一下,如果有其他建议,我会尝试做些什么 我有一个扁平的static.xml文件 我正在将事情转换为使用数据库,而不是加载这个flat.xml文件,这将是您常用的表单界面/GUI,它使用PHP/PDO提交到MySQL数据库,这里没有SQL注入;这已经很好了 我目前正致力于从flat.xml文件中获取数据的“备份日志”到数据库中 a。我曾尝试使用SQLLOADXMLINFILE:但不知道如何解析/转义特殊字符数据 b。我现在已经转到PHP/S
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 />';
}
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>
谢谢使用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>
听起来您的XML完全无效。你不应该解决那个问题吗?简单的撇号没有错。你能发布一个XML文件的例子吗?我同意Phil关于坏掉的XML和需要一个例子的观点,因为你可以看到需要转义的特定字符。您能够返回一个步骤并修复生成xml文件的任何内容吗?谢谢您的回复。是的,这就是我试图做的,在用PHP/SimpleXML解析XML之前“修复”XML。因此,替换前面提到的单引号/撇号的正则表达式问题是一个FLAT.xml文件。在任何编辑器中打开它添加新节点/元素保存。。不管我是退出还是替换它们……我无法让正则表达式正常工作,我不知道为什么这样做会有帮助……但下面是XML布局的一个片段:XML示例添加到上面的原始帖子中太长,无法发表评论我的正则表达式尝试只会产生失败的结果:无法成为ca&apos;[]'[]&apos;然而,其他几次寻找替代品的尝试似乎都失败了
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>