2012-03-19 79 views
0

我有很多這些文件jQuery1332199407617="01"需要刪除,但是,總是不同的一堆數字是,我可以刪除jQuery="(number is also always different)"之間的一切?PHP - 替換動態創建的內容

在此先感謝。根據要求的文件的一部分

樣品: (你可以看到它增加了一個jQuery的東西每次保存時間..因此需要將其刪除)

<H2 editing="false" revert="Projects:" jQuery1332198888840="12" jQuery1332199361841="12" jQuery1332199407617="12">ProjectsTesting</H2> <UL class=list1 jQuery1332198888840="17" jQuery1332199361841="17" jQuery1332199407617="17"> <LI jQuery1332198888840="16" jQuery1332199361841="16" jQuery1332199407617="16"><A href="#" jQuery1332198888840="15" jQuery1332199361841="15" jQuery1332199407617="15">Praesent vestibulum molestie </A> <LI jQuery1332198888840="19" jQuery1332199361841="19" jQuery1332199407617="19"><A href="#" jQuery1332198888840="18" jQuery1332199361841="18" jQuery1332199407617="18">Aenean nonummy </A> <LI jQuery1332198888840="21" jQuery1332199361841="21" jQuery1332199407617="21"><A href="#" jQuery1332198888840="20" jQuery1332199361841="20" jQuery1332199407617="20">Hendrerit mauris phasellus </A> <LI jQuery1332198888840="23" jQuery1332199361841="23" jQuery1332199407617="23"><A href="#" jQuery1332198888840="22" jQuery1332199361841="22" jQuery1332199407617="22">Porta fusce suscipit varius </A> <LI jQuery1332198888840="25" jQuery1332199361841="25" jQuery1332199407617="25"><A href="#" jQuery1332198888840="24" jQuery1332199361841="24" jQuery1332199407617="24">Cum sociis natoque</A> <LI jQuery1332198888840="27" jQuery1332199361841="27" jQuery1332199407617="27"><A href="#" jQuery1332198888840="26" jQuery1332199361841="26" jQuery1332199407617="26">Penatibus et magnis dis</A>I <LI jQuery1332198888840="29" jQuery1332199361841="29" jQuery1332199407617="29"><A href="#" jQuery1332198888840="28" jQuery1332199361841="28" jQuery1332199407617="28">Parturient montes</A> </LI></UL></DIV>

+0

我在想的preg_match但IDK的如何repalce事情吧:/我在正則表達式 – King 2012-03-19 23:33:37

+0

不能確定的東西實際上是源文件?不僅在瀏覽器呈現的DOM中? – 2012-03-19 23:34:17

+0

其在源文件中肯定..(該文件來自簡單保存的div) – King 2012-03-19 23:35:36

回答

2

這將刪除jQuery文本,不管是什麼數字。它還會去掉jQuery標籤被移除後留下的標籤末尾的過多空間。

$old = '<H2 editing="false" revert="Projects:" jQuery1332198888840="12" jQuery1332199361841="12" jQuery1332199407617="12">ProjectsTesting</H2> <UL class=list1 jQuery1332198888840="17" jQuery1332199361841="17" jQuery1332199407617="17"> <LI jQuery1332198888840="16" jQuery1332199361841="16" jQuery1332199407617="16"><A href="#" jQuery1332198888840="15" jQuery1332199361841="15" jQuery1332199407617="15">Praesent vestibulum molestie </A> <LI jQuery1332198888840="19" jQuery1332199361841="19" jQuery1332199407617="19"><A href="#" jQuery1332198888840="18" jQuery1332199361841="18" jQuery1332199407617="18">Aenean nonummy </A> <LI jQuery1332198888840="21" jQuery1332199361841="21" jQuery1332199407617="21"><A href="#" jQuery1332198888840="20" jQuery1332199361841="20" jQuery1332199407617="20">Hendrerit mauris phasellus </A> <LI jQuery1332198888840="23" jQuery1332199361841="23" jQuery1332199407617="23"><A href="#" jQuery1332198888840="22" jQuery1332199361841="22" jQuery1332199407617="22">Porta fusce suscipit varius </A> <LI jQuery1332198888840="25" jQuery1332199361841="25" jQuery1332199407617="25"><A href="#" jQuery1332198888840="24" jQuery1332199361841="24" jQuery1332199407617="24">Cum sociis natoque</A> <LI jQuery1332198888840="27" jQuery1332199361841="27" jQuery1332199407617="27"><A href="#" jQuery1332198888840="26" jQuery1332199361841="26" jQuery1332199407617="26">Penatibus et magnis dis</A>I <LI jQuery1332198888840="29" jQuery1332199361841="29" jQuery1332199407617="29"><A href="#" jQuery1332198888840="28" jQuery1332199361841="28" jQuery1332199407617="28">Parturient montes</A> </LI></UL></DIV>'; 

//This will erase all the jQuery strings. 
$new = preg_replace('/jQuery\d+="\d+"/', '', $old); 

//This will take out the extra spaces at the end of the tags that was left open. 
$new = preg_replace('/\s+>/', '>', $new); 

echo $new; 

欲瞭解更多信息,請參閱:http://php.net/manual/en/function.preg-replace.php

+0

dosent替代任何東西 – King 2012-03-19 23:47:22

+0

您可能在測試時犯了錯誤。我已經測試了代碼並驗證了它正在使用示例輸入。所有jQuery標籤和額外空間都從$ new中刪除。 – 2012-03-19 23:50:45

+0

啊,我不好,我不得不使用stripslashes :)現在它的工作原理:D:D tyvm – King 2012-03-19 23:50:57

2

如果我沒有理解您正確,這應該工作:

$myContent = preg_replace('/jQuery\d+="(\d+)"/g', 'jQuery="${1}"', $myContent); 

參見:http://php.net/manual/en/function.preg-replace.php

+0

呃我得到這個錯誤: 錯誤:警告:preg_match()[function.preg-match]:分隔符不能是字母數字或反斜槓 – King 2012-03-19 23:43:45

+0

正斜槓添加到正則表達式的開始和結束。確保你使用'preg_replace',而不是'preg_match'。 – Josh 2012-03-19 23:49:11

+0

糟糕。編輯修復。謝謝。 – Diego 2012-03-19 23:51:48