0

I am going to make a URL checking system.

I have this URL https://lasvegas.craigslist.org/mob/6169799901.html

Now I want to make this URL like this

https://lasvegas.craigslist.org/search/mob?query=6169799901

how can I do it using PHP?

3
  • 1
    I think you want parse_url() to consume the URL, then http_build_query() to construct it the way you want. You could try fiddling with string methods, but these two should work. Commented Jun 12, 2017 at 0:30
  • Add more details Commented Jun 12, 2017 at 0:33
  • @FirstOne - 3v4l.org/E6Y54 Scratch the http_build_query(), I meant pathinfo(). Commented Jun 12, 2017 at 0:46

2 Answers 2

1

Since I ended up (maybe?) solving it anyways, here's one method using URL/path parsing:

$url = 'https://lasvegas.craigslist.org/mob/6169799901.html';
$parsed = parse_url($url);
$basepath = pathinfo($parsed['path']);

echo $parsed['scheme'].
     "://".
     $parsed['host'].
     "/search".
     $basepath['dirname'].
     "?query=".
     $basepath['filename'];

Formatted for readability.

https://3v4l.org/E6Y54

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

Comments

0

Try this

$url = "https://lasvegas.craigslist.org/mob/6169799901.html";
$id = substr($url, strrpos($url, '/') + 1);
$id =  str_replace(".html","",$id);

$result = "https://lasvegas.craigslist.org/search/mob?query=".$id;
echo $result;

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.