Jump to content

Regex Help


atlanta

Recommended Posts

Hi guys i need a little help i need to retrieve different parts of a url for use in my .htaccess file

all help appreciated ..

 

ok heres my example..

http://domain.com/firstpart/secondpart/

 

ok i need to retrieve firstpart in one group and secondpart in the second group.. i tried

 

^/(.*?)/(.*?)/$

 

but the first group selects everything!

Link to comment
https://forums.phpfreaks.com/topic/140246-regex-help/
Share on other sites

Here's my take:

 

$str = parse_url('http://domain.com/firstpart/secondpart/');
$group = preg_split('#/#', $str['path'], -1, PREG_SPLIT_NO_EMPTY);
echo '<pre>'.print_r($group, true);

 

Output:

Array
(
    [0] => firstpart
    [1] => secondpart
)

 

When dealing with URLs, I like to personally use parse_url(), as it divides the url into organized components, and then simply deal with whatever component I need from there (which in this case happens to be the ['path'] part).

Link to comment
https://forums.phpfreaks.com/topic/140246-regex-help/#findComment-733858
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.