Solved

optimize a Preg_match rule

Posted on 2006-11-03
4
388 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 500 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

808 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