1

Could you please help me with converting $x_ary = split('&x=', $url); to the preg_split() equivalent?

4
  • sorry but why do u want to split the url instead of getting it using either get or post or request ? Commented Jun 4, 2011 at 14:49
  • Its a link a user inputs / read from database Commented Jun 4, 2011 at 15:11
  • @Prix: presumbly because it's not the current page's URL. But you can still parse it using parse_url followed by parse_str. Commented Jun 4, 2011 at 15:14
  • Its more complex than the example above , need to use preg_match if i used parse_url or explode instead of preg_split :) Commented Jun 4, 2011 at 15:17

1 Answer 1

3

In most cases you just need to add delimiters:

 preg_split('/&x=/',$url)

/ are fine if you do not need them as part of the pattern. And none of the other symbols are meta characters, so don't need escaping.

Take note that in your case you could just use explode instead, since you don't need a regex.

Sign up to request clarification or add additional context in comments.

4 Comments

explode may indeed be better suited here. it's definitely better performance if you're not doing anything complex...it's easily much faster than PCRE.
It might even be that OP could be interested in parse_str rather.
Actually the link is more difficult than it seems , otherwise i would have just used the explode but then i need to use preg_match on every value , so preg_split just saves some lines of code and performance hopefully
@Ronan by the way, don't be afraid to use a delimiter other than the slash. When working with URLs a lot, I find it far easier to flip to another character, just for readability's sake. (I hate looking at a hellish amount of backslashes) ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.