DevinFalgoust
DevinFalgoust

Reputation: 134

URL Replacement in PHP

I'm trying to change a value in a string that's holding my current URL. I'm trying to get something like

http://myurl.com/test/begin.php?req=&srclang=english&destlang=english&service=MyMemory

to look like

http://myurl.com/test/end.php?req=&srclang=english&destlang=english&service=MyMemory

replacing begin.php for end.php.

I need the end.php to be stored in a variable so it can change, but begin.php can be a static string.

I tried this, but it didn't work:

$endURL   = 'end.php';
$beginURL = 'begin.php';
$newURL   = str_ireplace($beginURL,$endURL,$url);

EDIT: Also, if I wanted to replace

http://myurl.com/begin.php?req=&srclang=english&destlang=english&service=MyMemory

with

http://newsite.com/end.php?req=&srclang=english&destlang=english&service=MyMemory

then how would I go about doing that?

Upvotes: 2

Views: 209

Answers (3)

Isaac.Lopez
Isaac.Lopez

Reputation: 65

Assuming that you want to replace the script filename of the url, you can use something like this :

<?php 
  $endURL   = 'end.php';
  $url ="http://myurl.com/test/begin.php?req=&srclang=english&destlang=english&service=MyMemory";
  $pattern = '/(.+)\/([^?\/]+)\?(.+)/';
  $replacement = '${1}/'.$endURL.'?${3}';
  $newURL = preg_replace($pattern , $replacement, $url);

   echo "url : $url <br>";
   echo "newURL : $newURL <br>";
?>

Upvotes: 1

Simon Forsberg
Simon Forsberg

Reputation: 13331

I recently made a PHP-file for this, it ended up looking like this:

$vars = $_SERVER["QUERY_STRING"];
$filename = $_SERVER["PHP_SELF"];
$filename = substr($filename, 4);
// for me substr removed 'abc/' in the beginning of the string, you can of course adjust this variable, this is the "end.php"-variable for you.

if (strlen($vars) > 0) $vars = '?' . $vars;
$resultURL = "http://somewhere.com" . $filename . $vars;

Upvotes: 0

Heath N
Heath N

Reputation: 563

How do you want them to get to end.php from beigin.php? Seems like you can just to a FORM submit to end.php and pass in the variables via POST or GET variables.

The only way to change what page (end.php, begin.php) a user is on is to link them to another page from that page, this requires a page refresh.

Upvotes: 0

Related Questions