Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

remove attribute tag

Posted on 2004-08-16
12
Medium Priority
?
328 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:Umesh
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:Umesh
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 

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:
Umesh earned 100 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to dynamically set the form action using jQuery.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

926 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