Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

optimize a Preg_match rule

Posted on 2006-11-03
4
Medium Priority
?
398 Views
Last Modified: 2012-06-22
I made this script to get the url and extract the value of the info variable. It works great, but I need to know if there is a better way to rewrite this to be a smaller amount of code. I think it is quite bulky looking.

It can be tested by simply using a url like this:
http://www.domain.com/somepage.ext?info=testdata&blah=moretests

It should only echo: testdata


$s = preg_match("/\\?info=(.*)([&])/i", $_SERVER['REQUEST_URI'], $matches);
      if ($s !== false)
            {
            $tmp = str_replace('&', '', $matches[0]);
            echo $info = str_replace('?info=', '', $tmp);
            }
0
Comment
Question by:ray-solomon
  • 2
  • 2
4 Comments
 
LVL 19

Expert Comment

by:VoteyDisciple
ID: 17871157
I'd suggest using the parse_url() method to get partway there.

$pieces = parse_url($url);
preg_match('/[&?]info=([^&]+)/', $pieces['query'], $match);
echo $match[1];
0
 
LVL 10

Author Comment

by:ray-solomon
ID: 17871204
I get this error:

Notice: Undefined offset: 1 in....

I have php 5 with register_globals=off if that helps.
0
 
LVL 10

Author Comment

by:ray-solomon
ID: 17871229
Here is what is contained in the parse_url array besides the path i modified

Array
(
    [path] => /whatever/whatever......
    [query] => info=testdata&blah=moretests
)
0
 
LVL 19

Accepted Solution

by:
VoteyDisciple earned 2000 total points
ID: 17871267
Whoops; I gave half of one solution and half of another.  I was thinking that info would come either after a ? or after an & but of course parse_url() doesn't return the ?

Let's make it:

preg_match(/(^|&)info=([^&]+)/', $pieces['query'], $match);
echo $match[2];

(Noting that I added a set of parens at the beginning so it's now $match[2], not [1].
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

877 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question