Page 1 of 1

Problem with ereg_replace, str_replace...doesn't replace!

Posted: Wed May 03, 2006 9:57 am
by holydevil
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi,

I want to remove stuff like <![if !supportEmptyParas]> from a .doc that has been saved as a htm.

I am having problems using ereg_replace or str_replace.

When I use:

Code: Select all

<?php

$filename = "deric_ortiz.htm";

if (!($fp = fopen($filename, "r+")))
{
die("cannot open ".$filename);
}

while ($data = fread($fp, 4096))
{

preg_match_all('/(<!).*?>/', $data, $matches);
foreach ($matches[0] as $match)
{
$data = ereg_replace($match, "", $data);

}


preg_match_all('/<.*?>/', $data, $matches);
foreach ($matches[0] as $match)
{
echo htmlspecialchars("$match ");
}
}

fclose($fp);
?>
I get the exact same output if I comment out the expression that uses eregi_replace or str_replace.

Basically my issue is that ereg_replace or str_replace is not actually replacing anything. Could this be an issue with how my web server is setup?

Anyone have any ideas?


After messing with this a bit more it just seems like I can't edit $data...is that because the variable is reading a file? If so how can I go about reading a file and creating a modified output from the file read? For instance what I want to do here is simply remove <! > tags and the stuff within them...it doesn't work at all because if I comment out the ('/<! ... ) preg match and the corresondping for each the output is exactly the same...I just get all the html tags... it doesn't clean out the tags I want at all...they appear too.


ANy ideas?

I am stumped.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Wed May 03, 2006 10:10 am
by feyd
Are you sure the first preg_match_all() is finding the tags in the first place?

Posted: Wed May 03, 2006 10:53 am
by holydevil
Yes!

If I do:

Code: Select all

preg_match_all('/(<!).*?>/', $data, $matches);
			foreach ($matches[0] as $match)
			{                        
                             echo htmlspecialchars("$match ");
                         }

Then it will output only the tags I wish to remove.

The issue is that the ereg_replace / str_replace aren't actually replacing the string.

Another weird thing is that I can't do a echo $data; when I do the page is just blank...

I think there is something about the nature of $data because it is a variable reading a file, that I am not understanding?

Posted: Wed May 03, 2006 11:51 am
by holydevil
Hmm... another question along these lines...I still dont think I understand reg expressions like I don't know what ~ and s are and I don't see mention of them in tutorials.

$data = preg_replace('~=~s', "=\"", $data);

...how would I go about encapsulating something in quotes? Like I find the string abc and I want it to change it to "abc" ... I don't think I would use preg_replace?

Posted: Wed May 03, 2006 12:02 pm
by feyd
have a look through our own regex tutorials and the regex posts I've linked to from Useful Posts.