2013-11-03 21 views
0

我在WordPress的帖子內容是一個很大的標記。它來自MS Word,因此它是由HTML嵌套標記和內聯樣式包裝的文本。

我有一段代碼在內容中重複多次(它代表文本腳註)。該段時,例如第一個腳註是:

<sup><a title="" href="file:///C:/Users/hp/Desktop/file.docx#_ftn1" name="_f 
tnref1"> 
<span class="MsoFootnoteReference"> 
    <span dir="LTR"> 
    <span class="MsoFootnoteReference"> 
    <span lang="EN-US" style="font-size: 16pt; line-height: 115%;"> 
    [1] 
    </span> 
    </span> 
    </span> 
</span> 
</a></sup> 
..... 

<a title="" href="file:///C:/Users/hp/Desktop/file.docx#_ftnref1" name="_ftn1"> 
<span class="MsoFootnoteReference"> 
    <span dir="LTR" lang="EN-US" style="font-size: 12.0pt; font-family: 'Simplified Arabic','serif';"> 
    <span class="MsoFootnoteReference"> 
    <span lang="EN-US" style="font-size: 12pt; line-height: 115%;"> 
    [1] 
    </span> 
    </span> 
    </span> 
</span> 
</a> 

我的目標是更改從2周的HREFs:

href="file:///C:/Users/hp/Desktop/file.docx#_ftn1" 

href="file:///C:/Users/hp/Desktop/file.docx#_ftnref1" 

到:

href="#_ftn1" 

href="#_ftnref1" 

,以便用戶可以跳轉從一個錨到另一個錨。

我使用的代碼是:

if(preg_match_all('/href\s*=\s*"[^"]+(#[^"]+)"/',get_the_content(),$match)) 
{ 

echo preg_replace('/href\s*=\s*"[^"]+(#[^"]+)"/','href=""', get_the_content()); 
} 

非常感謝你提前爲您的寶貴援助。

+0

請注意,HTML不能用正則表達式表示。使用像BeautifulSoup/libhtml5這樣健壯的HTML DOM解析器/處理器用於Python。 –

回答

1

找到解決方案。感謝您的時間。

if(preg_match_all('/href\s*=\s*"[^"]+(#[^"]+)"/',get_the_content(),$match)) 
    { 
    echo preg_replace('/href\s*=\s*"[^"]+(#[^"]+)"/','href="$1"', get_the_content()); 
    }