Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

remove attribute tag

Posted on 2004-08-16
12
Medium Priority
?
326 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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).

722 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