[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Extract string from PHP url

Posted on 2014-03-22
5
Medium Priority
?
821 Views
Last Modified: 2014-03-23
Hi

I need a php script that can redirect this url
www.abc.com/convert.php?name=NAME&surname=SURNAME&gender=MALE

to a new url as following (using the string in previous url)
www.xyz.com/output.php?n=NAME&s=SURNAME&g=MALE

can i know how can i achieve this?
I tried to google around and it made me confuse
Thanks!
0
Comment
Question by:Shirley80
5 Comments
 
LVL 35

Assisted Solution

by:Dan Craciun
Dan Craciun earned 800 total points
ID: 39947142
Put this at the beginning of convert.php:
<?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

HTH,
Dan
0
 

Author Comment

by:Shirley80
ID: 39947160
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
www.xyz.com/output.php?n=NAME&s=URNAME&g=MALE
(Note: I dont want the "S" in "Surname")
Thanks
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39947264
$surname = substr($_GET['surname'], 1);
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 600 total points
ID: 39947353
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.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html

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):
http://php.net/manual/en/function.urlencode.php

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.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_6630-Magic-Quotes-a-bad-idea-from-day-one.html

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

Open in new window

0
 
LVL 9

Assisted Solution

by:rinfo
rinfo earned 600 total points
ID: 39948310
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

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses
Course of the Month19 days, 9 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question