1

I have a url from where i am fetching value using GET method and i want to replace that value with the 4 digit random number in the string like

This is my main URL: http://localhost/ab/index.php?id=3345

These are the strings in my table (fetching from database):

http://anyurl/index.php?id=4876&abc=any
http://anyurl/index.php?id=8726&abc=any
http://anyurl/index.php?id=9026&abc=any

So whenever i open the main url the id's of the table should be replaced according to the main url

10
  • Do you want to replace string id with the id of main URL? Commented Dec 20, 2017 at 4:54
  • @AmitGupta the strings id should be replaced by main url id Commented Dec 20, 2017 at 4:56
  • ok fine why do you want to use str_replace or preg_replace instead of GET? Commented Dec 20, 2017 at 4:57
  • @AmitGupta the urls in the table are coming from database so whenever i open the main url the id of url coming from database should be changed according to the id of main url Commented Dec 20, 2017 at 4:58
  • Its not that much clear to me. Your main URL is also coming from database? Commented Dec 20, 2017 at 5:00

3 Answers 3

1

you can get the id parameter using global GET variable

$id = $_GET["id"]

then you can change the urls in the table according to it

$url = "http://anyurl/index.php?id=".$id."&abc=any"

Hope this will help you

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

6 Comments

the url coming from the database are all different with different id's there is no same pattern except the id is of 4 digit
can you explain it with an example. I' m not clear about it
anyurl/index.php?id=9026&abc=any this can be any thing and i am fething this from database.. The main url is of address bar. SO the id of the main url from the address bar should replace all the id's of the url coming from database
you want to change the table's urls according to the id parameter of the main url?
so then the approch is this
|
0

If you want to replace the id with preg_replace in string then you can do like below:

<?php
$string = 'http://anyurl/index.php?id=4876&abc=any';
$new_string = preg_replace('/[0-9]+/', $_GET["id"], $string);
echo $new_string;
// Will display http://anyurl/index.php?id=3345&abc=any 
?>

Comments

0

I know that it was asked years ago, but, probably, someone will find my solution helpful

So, I also offer to use preg_replace(), as Amit Gupta, but improve it for cases when you could have other numbers before ID value:

$url     = 'http://anyurl/index.php?foo=0713&id=4876&abc=any';
$new_id  = $_GET['id'];
// regex: catch 1 or more digits after 'id='
$new_url = preg_replace( '/id=(\d+)/', $new_id, $url );

If $_GET['id'] is 4920, for example, $new_url will be equal to http://anyurl/index.php?foo=0713&id=4920&abc=any

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.