2011-11-08 101 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... 
相关问题