www.webdeveloper.com
Results 1 to 3 of 3

Thread: regular expressions: finding a character o the end of string

  1. #1
    Join Date
    Oct 2010
    Posts
    11

    regular expressions: finding a character o the end of string

    Hi,

    Let's say I have a string (btw: this is a refferer to my site):

    Code:
    http://www.google.pl/search?somevar=someval&q=thisIsWhatIwant&q=f
    I'm using this code to get the "q" value, trying to make the query bolded:

    Code:
    $ref=preg_replace("/([&|?])q=(.*?)&/","$1q=<b>$2</b>&",$ref);
    But it doesn't work when "q" is the last value, that is when string is:

    Code:
    http://www.google.pl/search?somevar=someval&q=thisIsWhatIwant
    How do I get the "q" value, that is a string between "q=" and the end of the string?

  2. #2
    Join Date
    Sep 2010
    Posts
    160
    Code:
    $ref=preg_replace("/([&|?])q=([^&]*)(&?)/","$1q=<b>$2</b>$3",$ref);
    The above should work.

    Note that I also changed your "(.*?)" with "([^&]*)" since ".*" is not very effective. Also, the "?" mark at the end of your is also not useful since ".*" means zero or more and the question mark allows it to be zero, i.e. redundancy in the regexp.

    Hope it helps,

    Archie

  3. #3
    Join Date
    Oct 2010
    Posts
    11
    It works, thank you!

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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