Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jan 2013
    Posts
    76
    Thanks
    19
    Thanked 1 Time in 1 Post

    Modifying HTML tags with PHP

    I want to be able to modify an anchor tag with php after it is submitted by a user via a form. For instance, the text editor I'm implementing allows the addition of links, but I want to add a class to that link/anchor tag after it is submitted, before it goes into the database. Is this possible?

    So if the anchor tag submitted is:

    Code:
    <a href="www.somesite.com">www.somesite.com</a>
    I would like it to be saved into the database as:

    Code:
    <a href="www.somesite.com" class="normal">www.somesite.com</a>

    Can anyone help me with this? I've done a search on Google and all I get are results showing how to print out html with php. That's obviously not what I want.

  • #2

  • #3
    Regular Coder djh101's Avatar
    Join Date
    May 2009
    Location
    California
    Posts
    626
    Thanks
    51
    Thanked 67 Times in 67 Posts
    Regular expressions, if you are particular about class being placed at the end of the tag. Otherwise str_replace will work just fine (as in the above post).
    PHP Code:
    preg_replace('/<a href=\"([^>\"]*)\">/','<a href="$1" class="normal">',$text); 
    Regular-Expressions.info - Regex Tutorial, Examples and Reference - Regexp Patterns
    "Yeah science!"


  •  

    Posting Permissions

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