-1

I've got a string that is a url, e.g.

www.example.com/something/2/other_stuff/2/1

and i've got an int, let's say in this example to be 2.

i would like to make a new url replacing the last occurence of that int with its next. So, in the example, what i want is:

 www.example.com/something/2/other_stuff/3/1

The url can be written in any way, it has not a specific pattern. Once the new link is created, i would need also to check if it really exists on the web. Have you got any idea ?

Ethaan
  • 11,291
  • 5
  • 35
  • 45
Sanci
  • 119
  • 3
  • 13
  • http://php.net/parse_url + http://php.net/file_get_contents – Marc B Apr 06 '15 at 14:00
  • @MarcB if you try the parse_url with the url i used as example you can see it doesn't work properly – Sanci Apr 06 '15 at 14:09
  • so slap on an `http://` prefix and off you go. then it's just a matter of exploding the `['path']` portion and modifying as neeed. – Marc B Apr 06 '15 at 14:14

1 Answers1

0

Since you already have a string, and know which integer you want to check for the following solution should work.

//Make sure you sanitize the url
function yourAnswer($url, $yourInt){
 $offset = strrpos($url, $yourInt);
 if(!$offset)
    return FALSE; //The integer was not found in the url
 $url[$offset] = $yourInt + 1;
 $url = "http://".$url ; //Assuming only http

 // Now we check whether the page exists

 $file_headers = @get_headers($url);
 if($file_headers[0] == 'HTTP/1.1 404 Not Found')
    return FALSE;
 return TRUE;
 }
//In your function call make sure you quote the number, else strrpos looks for the character value of that number
//yourAnswer("www.example.com/something/2/other_stuff/2/1",'2');`

Further reference to on checking whether a file exists: How can I check if a URL exists via PHP?

Community
  • 1
  • 1
adarshdec23
  • 231
  • 2
  • 9