Page 1 of 1

preg_replace()

Posted: Thu Dec 15, 2005 5:30 pm
by DEXTER_c
Hello

It's my first post, so: I'm from Poland (Krakow), I'm 17 years old, I interesting in XHTML, CSS, JavaScript, PHP & MySQL.

I have a problem with this code:

Code: Select all

<?php
$code = '<style></style>'; // very many, about 1000 * '<style></style>'

function clean($code) {

   $code = str_replace('&', '&', $code);
   $code = str_replace("'", '& #39;', $code);
   $code = str_replace("<", "<", $code);
   $code = str_replace(">", ">", $code);
   $code = str_replace('"', '"', $code);
   $code = str_replace('=', '& #61;', $code);
   $code = str_replace('    ', '&nbsp;&nbsp;&nbsp;', $code);
   $code = str_replace('class', '& #99;& #108;& #97;& #115;& #115;', $code);
   $code = str_replace('span', '& #115;& #112;& #97;& #110;', $code);
   $code = str_replace('type', '& #116;& #121;& #112;& #101;', $code);
   $code = str_replace('value', '& #118;& #97;& #108;& #117;& #101;', $code);
     
   return $code;
}

$code = clean($code);

$code = preg_replace("/<style(.|\n)+<\/style>/e", "", $code);

echo $code;
?>
(I have added space between & and # for board)

I tested this code on 3 different servers. Browser (Mozilla) says: "Document haven't got any data". If $code is short, script works properly.

Posted: Thu Dec 15, 2005 5:57 pm
by Burrito
Moved to Regex

Posted: Thu Dec 15, 2005 5:58 pm
by Burrito
Welcome to Devnet!

fyi, you can use htmlentities() to replace most of your str_replace()'s.

as for your regex, are you just wanting to strip out the style attribute?

Posted: Sat Dec 17, 2005 9:32 am
by DEXTER_c
Burrito wrote:fyi, you can use htmlentities() to replace most of your str_replace()'s.
It don't change anything.
Burrito wrote:as for your regex, are you just wanting to strip out the style attribute?
1) What it's "regex"? :lol: I don't have this in my dictionary :wink:

2) I is element of big script. I cut this fragment which doesn't work.

Posted: Sat Dec 17, 2005 1:17 pm
by Chris Corbyn
Burrito is right, using htmlentities() can do all those replacements for you. Perhaps you could post your updated code?

1. Regex is short for "Regular Expession" which is the pattern you put in the first argument of your preg_replace() ;)

2. I think you'll need to post the new code so we can see whats going on :)

Posted: Sun Dec 18, 2005 5:15 pm
by DEXTER_c
New code:

Code: Select all

<?php
$code = '<style></style>'; // very many, about 1000 * '<style></style>'

function clean($code) {

   $code = htmlentities($code, ENT_QUOTES);
   $code = str_replace('=', '& #61;', $code);
   $code = str_replace('    ', '&nbsp;&nbsp;&nbsp;', $code);
   $code = str_replace('class', '& #99;& #108;& #97;& #115;& #115;', $code);
   $code = str_replace('span', '& #115;& #112;& #97;& #110;', $code);
   $code = str_replace('type', '& #116;& #121;& #112;& #101;', $code);
   $code = str_replace('value', '& #118;& #97;& #108;& #117;& #101;', $code);
     
   return $code;
}

$code = clean($code);

$code = preg_replace("/<style(.|\n)+<\/style>/e", "", $code);

echo $code;
?>