Forum Moderators: coopster

Message Too Old, No Replies

Auto append plus sign to dynamic url GET parameter

passing a name via GET

         

abushahin

10:40 pm on Mar 22, 2010 (gmt 0)

10+ Year Member



Ok so Im passing firstname and surname via url to retrieve related data from the db, the problem is that when it is passed its as white space between the first and the surname. i.e
something.php?n=john smith

Can anyone suggest a way to make this
something.php?n=john+smith

automatically add the plus sign in between.
usually the user clicks the link john smith which generates the GET parameter. any help appreciated.

IanKelley

10:44 pm on Mar 22, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I think I might be missing something, why not just change the links?

<a href="something.php?n=John+Smith">John Smith</a>

Readie

11:01 pm on Mar 22, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The following might work, bear in mind I've typed this OTF so it might have errors in:

$request = $_GET['n'];
$reg = '/([^\s]+) (.*)/is';
while(preg_match($reg, $request, $out)) {
$repl = $out[1] . '+' . $out[2];
$request = str_replace($out[1], $repl, $request);
}

IanKelley

12:34 am on Mar 23, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I got the impression he didn't want to use an intermediate script. If you did want to use a script in between, preg_match needs more memory and CPU than necessary for a split on spaces.

Instead something like this might make sense:

$name = implode('+',explode(' ',$name));

rocknbil

2:31 am on Mar 23, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$name=urlencode($name); [php.net]

Should fix spaces, and other things that need encoding for query strings. urldecode() to turn them back (which you shouldn't have to do on receipt of the get . . . )

<?php
header("content-type:text/html\n");
//Example only, uncleansed input
if ($_GET['n']) {
echo "Received: " . $_GET['n'] . "\n";
}
else {
$name="john smith";
echo "<p><a href=\"urlencode.php?n=" . urlencode($name) . "\">" . urlencode($name) . "</a></p>";
}
?>

Though you wouldn't encode the content between the anchors . . . just use $name.

Readie

9:59 am on Mar 23, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Instead something like this might make sense:

$name = implode('+',explode(' ',$name));

Yea I thought of that... Just quite some time after I'd posted and was already in bed, and I wasn't prepared to get out of bed again :P

abushahin

4:59 pm on Mar 23, 2010 (gmt 0)

10+ Year Member



Thanks for the great responses guys. Ive used the url encode although implode and explode method works. Only thing is if you urlencode the actual variable, it also echoes as encoded so although it adds the plus signs in the url it also echoes it as that. So ive encoded the actual variable into another and echoed the original.

$encname = urlencode($name);
<a href="urlencode.php?n=$encname">$name</a>