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 6 of 6
  1. #1
    New Coder
    Join Date
    May 2006
    Posts
    48
    Thanks
    1
    Thanked 0 Times in 0 Posts

    preg_replace href

    Hi,
    i'm trying to replace href in a tags using preg_replace,
    i only got this with preg_match.

    PHP Code:
    preg_match_all("/<\s*a\s+[^>]*href\s*=\s*[\"']?([^\"' >]+)[\"' >]/i"$content$match); 
    i don't familiar with regex, can anyone help me here?

  • #2
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,546
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Replace href with what? Or remove them?

  • #3
    New Coder
    Join Date
    May 2006
    Posts
    48
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Keleth View Post
    Replace href with what? Or remove them?
    I want to add http://example.com/script.php?site=HREF

  • #4
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,546
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Is this for user input? Or your own code? Basically, do you anticipate the input being possibly invalid (lack of double quotes, arbitrary spaces, etc)?

  • #5
    New Coder
    Join Date
    May 2006
    Posts
    48
    Thanks
    1
    Thanked 0 Times in 0 Posts
    PHP Code:
    $content preg_replace('#(<a\s[^>]*href)="([^:"]*)("|(?:(?:%20|\s|\+)[^"]*"))#'
    '$1="http://example.com/site='.$_GET(['site'].'/$2$3' ,$content); 
    the solution in case someone needs it

  • #6
    New Coder
    Join Date
    May 2006
    Posts
    48
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Ohh,
    that's only works on double quotes, can someone help me out


  •  

    Posting Permissions

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