2014-05-06 39 views
1

我認爲使用正則表達式來查找/替換是我最好的選擇這個..但我會給我一個概述,我想要做的事情,如果有一些其他的建議/建議正則表達式+標記之間替換字符使用記事本++

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

  2. 我將事情交給使用一個數據庫,而不是裝載這款平板.xml文件,(這將是您的通常形式的接口/圖形用戶界面,提交到MySQL數據庫使用PHP/PDO(這裏沒有SQL注入朋友!);)(這已經工作正常)

  3. 我目前正致力於將flat .xml文件中的數據「back-log」存入數據庫中..

    a。我試圖使用SQL LOAD XML INFILEhttps://stackoverflow.com/questions/22775206/how-to-use-load-xml-infile-with-special-characters但無法弄清楚如何解析/轉義特殊字符數據...

    b。現在我已經轉移到了PHP/SimpleXML,但我在XML中的某些節點/元素中再次遇到了特殊字符。 (可以是單或雙引號,「&」的標誌,不知道..它是一個「說明」字段)

當我嘗試加載XML文件..我得到一個錯誤:

Warning: simplexml_load_file() [function.simplexml-load-file]: xml_source.xml:142: parser error : Opening and ending tag mismatch: BR line 142 and description in C:\wamp\www\xml_tests\simpleXML_test.php on line 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 />'; 
} 

簡單的測試,但如上所述,我得到上面的錯誤仍然在那裏的撇號。

如何使用正則表達式來搜索特殊字符(單引號/撇號),這是兩者之間<tags> </tags>

這是我曾嘗試在搜索部分正則表達式。(我似乎無法敲定替換部分由於某種原因用整數替換整個單詞?)

搜索:(記事本++)

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

替換:XML的

\&apos; 

例如:

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

感謝

+0

聽起來像是你的XML是根本無效的。你不應該解決這個問題嗎?簡單的撇號沒有任何問題。你能發佈一個XML文件的例子嗎? – Phil

+1

我同意Phil關於破xml和要求的例子,正如你可以看到[這裏](http://en.wikipedia.org/wiki/XML#Escaping)特定字符需要被轉義。你能夠回去一步並修復生成XML文件的任何內容嗎? –

+0

感謝您的回覆。 是的,這是我想要做的,在用PHP/SimpleXML解析之前'修復'XML。 (因此,替換單引號/撇號的正則表達式問題) 如前所述,這是一個FLAT .xml文件。在任何編輯器打開它添加一個新的節點/元素保存).. 無關緊要,如果我逃脫然後或替換它們..(我不能得到正則表達式工作) 我不知道爲什麼這將幫助。 ..但這裏是XML佈局的一個片段: – whispers

回答

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> 
相關問題