preg_replace()

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
DEXTER_c
Forum Newbie
Posts: 3
Joined: Thu Dec 15, 2005 5:03 pm
Location: Poland

preg_replace()

Post 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.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

Moved to Regex
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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?
User avatar
DEXTER_c
Forum Newbie
Posts: 3
Joined: Thu Dec 15, 2005 5:03 pm
Location: Poland

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 :)
User avatar
DEXTER_c
Forum Newbie
Posts: 3
Joined: Thu Dec 15, 2005 5:03 pm
Location: Poland

Post 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;
?>
Post Reply