3

I am creating a PHP proxy where it accepts a url and confirms it is on my list of servers.

When importing the url from the application i ran it to an issue where i needed 2 parser tags. i need it to split along a "\?" tag as well as a string, in my case, "export?"

i am using preg for the first tag. Does this accept the strings like my export tag or is there some other method for doing this?

please le me know how this is accomplished or if you have more questions.

crazywill32
  • 390
  • 1
  • 4
  • 12
  • 1
    What have you tried so far? Could you add some code, maybe some input strings and the regular expression you're using? – Spiny Norman Dec 20 '10 at 16:20
  • 8
    Have you tried [`parse_url`](http://us3.php.net/parse_url) instead of regular expressions? – ircmaxell Dec 20 '10 at 16:21
  • I have attempted to the the preg_split method that was included in the origional proxy. the biggest problem stems from the fact that my urls are like http://www.domain.com/subfolders/? or http://www.domain.com/subfolders/export? – crazywill32 Dec 20 '10 at 16:31

4 Answers4

14

As ircmaxell has already stated in the comments, PHP does already have a function to parse a URL: parse_url.

And when you have the URL path (I assume your export? the path suffix plus the query indicator), you can use explode to split the path into its path segments:

$path = parse_url($url, PHP_URL_PATH);
$segments = explode('/', $path);

You can then get the last path segment with one of the following:

end($segments)
$segments[count($segments)-1]

And to cope with trailing slashes, you can use rtrim($path, '/') to remove them.

All together:

$url = 'http://www.example.com/subfolders/export?';
$path = parse_url($url, PHP_URL_PATH);
$segments = explode('/', rtrim($path, '/'));
echo end($segments);
Community
  • 1
  • 1
Gumbo
  • 643,351
  • 109
  • 780
  • 844
  • that doesn't seem to be working for me. it is not allowing me to see the result of your sample code. What would happen to the string if there is a JSON string after the export? – crazywill32 Dec 20 '10 at 16:53
  • @user548869: Oh yes, you’re right, `array_slice` returns an array. Try one of the other variants instead. – Gumbo Dec 20 '10 at 16:56
0

A regular expression should do the trick, something like the below would work. This is what Django uses in their URL dispatcher

r'^export/$'
Rasiel
  • 2,823
  • 6
  • 31
  • 37
  • 1
    This doesn't really help too much. Does not split on the pattern and only targets URLs the are not absolute (`^` is beginning of line and `$` is end of line). – John Giotta Dec 20 '10 at 16:31
0

Regular expressions are strings matches that may also include variable matches. Because ? is included within ?, you have to do your split twice. Once on export? first, and a second pass on each of those with ? as your delimiter. As written below, you're just splitting on either of two different strings.

$first = preg_split('export\?', ...);
for ($first) {
        array_push ($second,preg_split('\?', ...)');
}

That isn't perfectly valid PHP, but I hope it is close enough pseudocode.

Jeff Ferland
  • 17,832
  • 7
  • 46
  • 76
0

Hey guys i ended up using an explode which looked for the string (export?) and then i used the preg split command to search for the \?. this provided me with the protion i was looking for. thanks guys.

crazywill32
  • 390
  • 1
  • 4
  • 12