2013-11-03 22 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()); 
    }