www.webdeveloper.com
Results 1 to 3 of 3

Thread: Is this preg_replace correct ?

  1. #1
    Join Date
    May 2006
    Posts
    245

    Is this preg_replace correct ?

    Hi,

    The HTML5 validator doesn't like '&' in the urls it wants '&'
    so I am trying to change them before they get output from the server.

    I can not stop users from pasting if urls with '&' in them so I just want to swap them out.

    Because I only want to change '&' that are in urls - not anywhere else
    I need to match a pattern.

    So I have my preg_replace like this:

    $text= preg_replace("/[href(.+?)][&](.+?)/s",'href'.$1.'&'.$2,$text);

    Dunno why - just doesn't look right


    Hopefully I will get the hang of this one day !!


    Thanks.
    Developers Choice Revealed:
    www.devchoice.info
    Which host has won, and why ?

  2. #2
    Join Date
    Nov 2013
    Posts
    33
    wouldn't str_replace Be a lot easier?

    PHP Code:
    str_replace('&''&''http://www.example.com/search.php?query=Hello&world&this&is&a&test'); 

  3. #3
    Join Date
    May 2004
    Location
    chennai, tamil nadu, India
    Posts
    437
    Quote Originally Posted by jeddik View Post
    Dunno why - just doesn't look right


    Hopefully I will get the hang of this one day !!


    Thanks.
    You could test your regular expression online using testers like http://gskinner.com/RegExr/
    Chris, Senior Developer,
    www.Chrisranjana.com

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles