Jump to content

regex solution not working for stripping our javascript tags


paulintodarkness

Recommended Posts

Trying to strip out all from javascript tag except for src url, but all regex i've tried, regex buddy etc doesn't seem to return what i am looking for..hit a wall, can anyone help?

 

Basically I need to do this:

I submit a form:

I send the form information to a page which captures the form textarea content in a variable called $ex_js which I get as a post like this:

 

$ex_js = $_POST["content"];

 

if the contents in $ex_js contain a javascript tag like this:

<script type="text/javascript" src="http://www.abc.com"></script>

 

I want to strip out EVERYTHING except for http://www.abc.com and return in a variable named $result so when i echo $result it's simply just http://www.abc.com

 

Thanks

Link to comment
Share on other sites

$_POST["content"] = 'some garbage......<script type="text/javascript" src="http://www.abc.com"></script>....more garbage...'; // form content...
$ex_js = preg_replace('#<script type=[\'"]text/javascript[\'"].+?src=[\'"]([^\'"]+)[\'"].*?>.*?</script>#i' , '$1', $_POST["content"]);
echo $ex_js;

 

 

Link to comment
Share on other sites

Thanks,

Sorry a bit of a novice to this.

Not sure how to apply this.

 

Here is the code i am using:

<?

$ex_js = $_POST["content"];

?>

<html>

<head>

<title>page</title>

</head>

<body>

<form method="post" action="<? echo $_SERVER['PHP_SELF']; ?>">

<input type="hidden" name="content">

<textarea cols="85" rows="6" name="content">

</textarea>

<input type="submit" value="Validate" />

</form>

</body>

</html>

 

Where should I apply this code?

 

$_POST["content"] = 'some garbage......<script type="text/javascript" src="http://www.abc.com"></script>....more garbage...'; // form content...

 

$ex_js = preg_replace('#<script type=[\'"]text/javascript[\'"].+?src=[\'"]([^\'"]+)[\'"].*?>.*?</script>#i' , '$1', $_POST["content"]);

echo $ex_js;

 

I want to strip the javascript tag when the form posts. tried this:

 

Added this in the form's textarea:

some garbage......<script type="text/javascript" src="http://www.abc.com"></script>....more garbage...

 

Submitted the form, had this code to capture the above in the post.

 

$stuff = $_POST["content"];

 

$ex_js = preg_replace('#<script type=[\'"]text/javascript[\'"].+?src=[\'"]([^\'"]+)[\'"].*?>.*?</script>#i' , '$1', $_POST["content"]);

echo $ex_js;

 

Doesn't seem to work.

 

Am i doing something wrong?

Thanks for you help

 

 

Link to comment
Share on other sites

I supplied the meat and potatoes, you supply the gravy.

When posting on these boards, it is assumed that you have general working knowledge of PHP. Not to be difficult, impolite nor rude, but I would suggest you take a step back and advance yourself in PHP. There are plenty of books / online tutorials and other resources available at your disposal.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.