Extract string from PHP url

Posted on 2014-03-22
Last Modified: 2014-03-23

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
Question by:Shirley80
LVL 34

Assisted Solution

by:Dan Craciun
Dan Craciun earned 200 total points
ID: 39947142
Put this at the beginning of convert.php:
    $name = $_GET['name'];
    $surname = $_GET['surname'];
    $gender = $_GET['gender'];
    header( "Location:$name&s=$surname&g=$gender" ) ;

Open in new window


Author Comment

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
(Note: I dont want the "S" in "Surname")
LVL 34

Expert Comment

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

Accepted Solution

Ray Paseur earned 150 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.

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.

= ''
. '?n=' . urlencode($_GET['name'])
. '&s=' . urlencode(substr($_GET['surname'],1))
. '&g=' . urlencode($_GET['gender'])
header("Location: $url");

Open in new window


Assisted Solution

rinfo earned 150 total points
ID: 39948310
you may use parse_url
  $url = "";

	$urlArr = parse_url($url);

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

    //current specific line
	$out = "";

	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


Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
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.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

911 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now