簡體   English   中英

正則表達式+使用記事本++替換標簽之間的字符

[英]regex + replace character between tags using Notepad++

我認為使用正則表達式來查找/替換是我的最佳選擇..但如果有其他建議/建議,我將概述我要嘗試的操作

  1. 我有一個FLAT(靜態).xml文件

  2. 我正在將事情轉換為使用數據庫,而不是加載這個平面的.xml文件(這將是您通常的表單接口/ GUI,該界面/ 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的某些節點/元素中使用特殊字符再次遇到麻煩。 (不確定是單引號還是雙引號,“&”符號。。它是“描述”字段)

當我嘗試加載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搜索兩個< 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>

謝謝

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM