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.

3
  • 1
    What have you tried so far? Could you add some code, maybe some input strings and the regular expression you're using? Commented Dec 20, 2010 at 16:20
  • 8
    Have you tried parse_url instead of regular expressions? Commented Dec 20, 2010 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 domain.com/subfolders? or domain.com/subfolders/export? Commented Dec 20, 2010 at 16:31

4 Answers 4

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);
Sign up to request clarification or add additional context in comments.

2 Comments

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?
@user548869: Oh yes, you’re right, array_slice returns an array. Try one of the other variants instead.
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/$'

1 Comment

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).
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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.