Solved

optimize a Preg_match rule

Posted on 2006-11-03
4
385 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
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…
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 a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now