0

I have a variable in my PHP code where its equal to a stored string. A small portion of this string is taken and used as a preview on a different page. On occasion there will be Javascript in that string, how could I say the following:

Pseudocode:
if stringVar contains "<script"
then remove the substring starting at "<script" and ending at "/script>"
2
  • Are you sure that within that text, you will NEVER find another instance of "<script", or "/script>" before the actual ends? If you are, it sounds like this would be trivial to do with a regex replace. P.S. This sounds suspiciously like trying to parse HTML - if that's the case, don't do it; use an XML parser instead. Commented Nov 8, 2011 at 1:05
  • Are you by chance looking for strip_tags? Commented Nov 8, 2011 at 1:15

2 Answers 2

1

If you're just getting rid of script tags:

$result = preg_replace('%<script>.*</script>%i', '', $subject);
Sign up to request clarification or add additional context in comments.

Comments

0

strpos function returns the position of one string in another,

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

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

after that it's just substr to get everything before and after.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.