Solved

optimize a Preg_match rule

Posted on 2006-11-03
4
389 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to dynamically set the form action using jQuery.
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.

749 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