Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regular Expressions!!!

Status
Not open for further replies.

vbkris

Programmer
Joined
Jan 20, 2003
Messages
5,994
Location
IN
o.k
i have an html file that goes like this:
Code:
<html>
<head>
<script>
//Function2
</script>
</head>
<body>
Hello how are you
<script>
//Function2
</script>
</body>
</html>
[/code]

using PHP i read through the file and store it in a single variable called $val.

i want to remove the <script> tags and their contents(ie function1 and 2).

i used the following:
$val1=eregi_replace(&quot;<script[^>]*>.*</script>&quot;,&quot;&quot;,$val);

the problem here is that it replaces from the first scrtip tag (ie function1) upto the last </script> tag ie function2. therefore the value in $val becomes:

Code:
<html>
<head>
</body>
</html>
[/code]


but i want:
Code:
<html>
<head>
<body>
Hello how are u
</body>
</html>
[/code]

Note: I have found a roundabout for this. but i was just wondering why this peculiar behavior?


Known is handfull, Unknown is worldfull
 
I'd use preg_replace with the U modifier.
By default PCRE .* is greedy, I assume same for the ereg's.
When you tell the pattern to be ungreedy it will resolve .* to the smallest number of characters between the delimiters. That should yield the desired result.
 
can i have the preg code? i have no idea what the u means...

Known is handfull, Unknown is worldfull
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top