0

In the following URL

http://www.example.com/usa-florida-1234

Can I split the URL with 3 parts in the URL as USA, Florida and 1234??

I just need to do this like a Query String Method for

Sample Content Like

My content is posted is USA in the city of florida and the post code is 1234

How can I split the URL within - dashes

like

<?php
$country = // What to keep here?
$city =  // What to keep here?
$code = // What to keep here?
?>

My content is posted is <?php echo $country; ?> in the city of <?php echo $city; ?> and the post code is <?php echo $code; ?>

Thanks

0

6 Answers 6

4
list($country, $city, $code) =
    explode('-', ltrim(parse_url($url, PHP_URL_PATH), '/'));

No need for a regular expression here.

If the path contains more parts, country is going to end up with some undesired path parts.

$country = end(explode('/', $country));
Sign up to request clarification or add additional context in comments.

3 Comments

I like smart solutions but this one seems to do too much in a single line of code. You can get too verbose though (see my answer).
All good but I am getting 'test/usa' for country when i try to run as localhost/test/usa-florida-1234
@lock I didn't realize that there were more parts to the path than just the information you needed; this does change the answer a bit. I'll update it.
1

You can use:

$arr = parse_url("http://localhost/test/usa-florida-1234");
print_r(explode('-', preg_replace('#^.*?/([^/]+)$#', '$1', $arr['path'])));

OUTPUT:

Array
(
    [0] => usa
    [1] => florida
    [2] => 1234
)

2 Comments

Array ( [0] => test/usa [1] instead of Array ( [0] => usa [1]
when I try to run as localhost/test/usa-florida-1234 Array [0] coming as test/USA
1
<?php

preg_match('|http://www.example.com/(.*)-(.*)-(.*)|', 'http://www.example.com/usa-florida-1234', $matches);

print_r($matches);


$country = $matches[1];
$city =  $matches[2];
$code = $matches[3];

UPDATE:

Each character in a regular expression is either understood to be a metacharacter with its special meaning, or a regular character with its literal meaning.

The pattern is composed of a sequence of atoms. The simplest atom is a literal, but grouping parts of the pattern to match an atom will require using ( ) as metacharacters.

When you match a pattern within parentheses, you can use any of $1, $2, ... or \1 \2 ...(depending on the matcher!) later to refer to the previously matched pattern.

in the example above:

| (pipe) - in the example above defines start and the end of the pattern

() - both meta-characters ( and ) a grouped parts of the pattern

. (dot) = meta-character; matches any single character

* (asterisk) = meta-character, quantifier; defines how often that preceding element(character or group, sub expression) is allowed to occur

Having that in mind:

(.*) resolves as a match for later reference a group made of any characters that occur zero or multiple times

And :

|http://www.example.com/(.*)-(.*)-(.*)|

Equals to match:

Any occurrence in the text of a string that contains the string http://www.example.com/ followed by any character zero or multiple times before the - character than again followed by any character zero or multiple times before - character followed by any character zero or multiple times.

Comments

0

You first need to obtain the path from your URL ('usa-florida-1234'). Then you need to split it by the dash char.

For the first part you can use parse_url. You'll find that it returns a string starting with an '/' also. You'll need to trim it. Next you can use explode.

$urlParts = parse_url('http://www.testdomain.com/usa-florida-1234');
$urlPath = ltrim($urlParts['path'], '/');
$addressParts = explode('-', $urlPath);
$country = $addressParts[0];
$city = $addressParts[1];
$code = $addressParts[2];

You should also check that there actually are three strings in the array that split returns. Some error checking is always good.

A better solution would be to use an MVC library where you define routes

'addressRoute' => '/{country}-{state}-{id}'

and handlers for them

function addressRoute($country, $state, $id) {
}

There are lots of discussions on this subject.

Comments

0

pathinfo and get the basename from the array

  <?php
    $var="http://www.testdomain.com/usa-florida-1234";
    $arr=pathinfo($var);
    echo $arr['basename'];
    $arr=explode("-",$arr['basename']);
    print_r($arr);

OUTPUT:

Array
(
    [0] => usa
    [1] => florida
    [2] => 1234
)

Comments

-1

If all of the URLs used are to be expected in that format, you could use an explode() there.

$url = "http://www.example.com/usa-florida-1234";
$parts = explode('-', $url);
$country = $parts[0];
$city = $parts[1];
$code = $parts[2];

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.