2011-11-08 121 views
0

我在我的PHP代碼中有一個變量,其中它等於存儲的字符串。 該字符串的一小部分被用作另一頁上的預覽。 有時會出現的Javascript在該字符串,我怎麼可能說以下內容:從字符串中刪除子字符串,其中只有字符串的開頭和結尾已知

Pseudocode: 
if stringVar contains "<script" 
then remove the substring starting at "<script" and ending at "/script>" 
+0

你確定該文本中,你會發現_永遠_「<腳本」,或者另一個實例「/ SCRIPT>」在實際結束之前?如果你是這樣的話,聽起來這對於用正則表達式替換來說是微不足道的。附:這聽起來像想[解析HTML](http://stackoverflow.com/q/1732348/540162) - 如果是這樣的話,不要這樣做;改爲使用XML解析器。 – Nightfirecat

+0

你是否偶然尋找['strip_tags'](http://php.net/strip_tags)? – deceze

回答

1

如果你剛剛擺脫腳本標籤:

$result = preg_replace('%<script>.*</script>%i', '', $subject); 
0

strpos函數返回另一個字符串中的位置,

so your start is strpos($SomeString,"<script") 

end is strpos($SomeString,"/script>") + 7 

之後,它只是SUBSTR得到之前和之後的一切。

Note this will not work if <script can contains the <Script /Script> tags... 
相關問題