Extract string from PHP url


I need a php script that can redirect this url

to a new url as following (using the string in previous url)

can i know how can i achieve this?
I tried to google around and it made me confuse
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dan CraciunIT ConsultantCommented:
Put this at the beginning of convert.php:
    $name = $_GET['name'];
    $surname = $_GET['surname'];
    $gender = $_GET['gender'];
    header( "Location: http://www.xyz.com.com/output.php?n=$name&s=$surname&g=$gender" ) ;

Open in new window

Shirley80Author Commented:
That's fast! Five Stars!
Just one last question, how can i skip the first character and only start to extract from the second character of "SURNAME"

Result will be
(Note: I dont want the "S" in "Surname")
Dan CraciunIT ConsultantCommented:
$surname = substr($_GET['surname'], 1);
Ray PaseurCommented:
Some good "getting started" resources are available in this article.  Don't use Google search to find PHP examples -- there are too many old and incompetently written samples lying around the internet.  It's easy to be confused by dreck.

PHP automatically runs urldecode() functions on the URL arguments.  Before you use them in another URL, you want to urlencode() the values.  Man page here (check the Notes):

Also, if you find that you have magic quotes enabled, you may need stripslashes().  But magic quotes should be off in any modern PHP installation.

= 'http://www.xyz.com.com/output.php'
. '?n=' . urlencode($_GET['name'])
. '&s=' . urlencode(substr($_GET['surname'],1))
. '&g=' . urlencode($_GET['gender'])
header("Location: $url");

Open in new window


Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
you may use parse_url
  $url = "www.abc.com/convert.php?name=NAME&surname=SURNAME&gender=MALE";

	$urlArr = parse_url($url);

	$qry = explode("&",$urlArr[query]);

    //current specific line
	$out = "www.xyz.com/output.php?";

	foreach($qry as $q)
		$bits = explode('=', $q);
		$key  = $bits[0];
		$val  = $bits[1];

		//current specific code
		if($key == "name")
			$out .= "n=".$val."&";
		if($key == "surname")
			$out .= "s=".$val."&";
		if($key == "gender")
			$out .= "g=".$val;


	echo $out;

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.