Solved

remove attribute tag

Posted on 2004-08-16
12
318 Views
Last Modified: 2013-11-19
Hi

I try to clean all the attributes at the tags "P" AND "DIV" except align=* <DIV>

Here is the code:
<?

/// THIS WORK
$data = '===<p er=gg a="b" align=center x="y" er=gg>===';
$data = preg_replace("/<(p|div)[^>]*( align=[^\s>]+)[^>]*>/i", "<$1$2>", $data );
echo "<textarea cols=80 rows=5>$data</textarea>\n";

/// THIS WORK
$data = '===<p er=gg a="b" align="center" x="y" er=gg>===';
$data = preg_replace("/<(p|div)[^>]*( align=[^\s>]+)[^>]*>/i", "<$1$2>", $data );
echo "<textarea cols=80 rows=5>$data</textarea>\n";

/// THIS DO NOT WORK
$data = '===<p er=gg a="b" x="y" er=gg>===';
$data = preg_replace("/<(p|div)[^>]*( align=[^\s>]+)[^>]*>/i", "<$1$2>", $data );
echo "<textarea cols=80 rows=5>$data</textarea>\n";


?>
</body>
</html>
0
Comment
Question by:bogmar
  • 4
  • 3
  • 3
  • +1
12 Comments
 
LVL 26

Expert Comment

by:ushastry
ID: 11810222
Try this..

<?

/// THIS WORK
$data = '===<p er=gg a="b" align=center x="y" er=gg>===';
$data = preg_replace("/<(p|div)[^>]*( align=[^\s>]+)[^>]*>/i", "<$1$2>", $data );
echo "<textarea cols=80 rows=5>$data</textarea>\n";

/// THIS WORK
$data = '===<p er=gg a="b" align="center" x="y" er=gg>===';
$data = preg_replace("/<(p|div)[^>]*( align=[^\s>]+)[^>]*>/i", "<$1$2>", $data );
echo "<textarea cols=80 rows=5>$data</textarea>\n";

/// THIS DO NOT WORK
$data = '===<p er=gg a="b" x="y" er=gg>===';
$data = preg_replace("/<(p|div)[^>]*( align=[^\s>]+)*[^>]*>/i", "<$1$2>", $data );
echo "<textarea cols=80 rows=5>$data</textarea>\n";


?>
0
 
LVL 26

Expert Comment

by:ushastry
ID: 11810286
Earlier it was assuming that the attribute align will be there in the <P|Div tag.. I have suffixed the * to ( align=[^\s>]+)..


Hope this Helps!
0
 

Author Comment

by:bogmar
ID: 11810296
Thanks but you cheated a little bit. The idea is to make it work for any $data variable.
0
 
LVL 48

Expert Comment

by:hernst42
ID: 11810343
This is a very difficult thing, to do as regex, because if you use
/<(p|div)[^>]*( align=[^\s>]+)?[^>]*>/i
for all data the [^>]* of the regex-machine eats all characters. It should stop if it find the keyword allign. Such things can be done with regular expressions and conditions, but haven't found out how excatly to use that
0
 
LVL 48

Expert Comment

by:hernst42
ID: 11810379
An easy way would be to spilt this up into two str-repaces. The one how you do it at the moment. The second and first to use, for d and div-tags which do not contain the align like:
/<(p|div)[^>]>/i
0
 

Author Comment

by:bogmar
ID: 11810416
let me know if you find a solution.
I will accept any working solution that will work for ANY $data variable.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:bogmar
ID: 11810471
I am sorry hernst42 but I don’t think that I understood correctly.
Can you please provide the code?
0
 
LVL 10

Expert Comment

by:eeBlueShadow
ID: 11810731
untested:

$data = preg_replace("/<(p|div)[^>]*?( align=(['\"])\w+\\3)[^>]*>/i", "<$1$2>", $data);
0
 
LVL 10

Expert Comment

by:eeBlueShadow
ID: 11810826
oops, I tested it and it doesn't work, lemme keep working on it.
0
 
LVL 48

Expert Comment

by:hernst42
ID: 11810872
I thougth with two str_replaces like:

$regex = array("/<(p|div)([^>]*>/i", "/<(p|div)([^>]*)?( align=[^\s>]+)[^>]*>/iu");
$replace = array("<$1>", "<$1$2>");

$data = preg_replace($regex, $replace, $data );
echo "<textarea cols=80 rows=5>$data</textarea>\n";

could work, but it will also replace the align :-( So it seems to me that you have conditions in the regex or go a complete other way to remove all tags except the align
0
 
LVL 26

Accepted Solution

by:
ushastry earned 50 total points
ID: 11811122
Check out this..

$data = '===<p er=gg a="b" x="y">===';

if(eregi("align=",$data))
{
    $data = preg_replace("/<(p|div)[^>]*( align=[^\s>]+)[^>]*>/i", "<$1$2>", $data );
}else{

    $data = preg_replace("/<(p|div)[^>]*>/i", "<$1>", $data );
}


echo "<textarea cols=80 rows=5>$data</textarea>\n";
0
 
LVL 48

Expert Comment

by:hernst42
ID: 11812947
ok doing it via a regex is nearly impossible. So the following code will work, even if those settings are mixed up.

function sanitize($id, $args) {
    if (preg_match('/(align=[^\s>]*)/i', $args, $m)) {
        $m[1] = preg_replace('/\\\\\"/', '"', $m[1]);
        $m[1] = preg_replace('/\\\\/', '\\', $m[1]);
        return "<$id " . $m[1] . ">";
    }
    return "<$id>";
}

$data = '===<p er=gg a="b" align=center x="y" er=gg>===i ===<p er=gg a="b" x="y" er=gg>===';
$data = preg_replace("/<(p|div)([^>]*)>/ie", "sanitize('$1', '$2')", $data );
echo "<textarea cols=80 rows=5>$data</textarea>\n";

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
The viewer will learn how to count occurrences of each item in an array.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now