Results 1 to 6 of 6

Thread: how to remove attribute on an element?

  1. #1
    Join Date
    Jan 2010
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default how to remove attribute on an element?

    Please help !!
    How to remove attribute (href, style, id, class, name, title, etc.) on the elements <a> <p> <div> using php ?

    For example:
    <p style="font:10px arial;background-color:#f3f3f3; etc.." title=".." name=".."> remove attribute </ p>

    Output:
    <p> remove attribute </ p>

  2. #2
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    I've had success with this function: http://www.php.net/manual/en/functio...tags.php#96483
    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

  3. #3
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    The easiest way I think is

    PHP Code:
    $code preg_replace('/<(div).*?>|<(a).*?>|<(p).*?>/s'"<$1$2$3>"$code); 
    The '.*?' says find anything until (. means any single charachter) (* means find all preceding characters in this case any so it is infinite until....) (? means find the first occurrence of the trailing character in this case >) and the '|' is or.
    Last edited by bluewalrus; 12-28-2010 at 08:52 PM.
    Corrections to my coding/thoughts welcome.

  4. The Following User Says Thank You to bluewalrus For This Useful Post:

    ampera (12-28-2010)

  5. #4
    Join Date
    Jan 2010
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    @bluewalrus : very simple .. thanks for the solution ...

  6. #5
    Join Date
    Dec 2010
    Location
    Hyderabad, India
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    Thanks bluewalrus for the detailed explanation. You even gave the explanation for the regular expression. Top stuff!

  7. #6
    Join Date
    Jan 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    bluewalrus thanks, I'm looking for long time after this regular expression.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •