|
Posted by emmakane on 01/01/06 22:05
I am teaching myself PHP and am I am stuck trying to do the following.
I have the following code which pulls all the names of items in my
database and puts them in a hyperlinked list. I then want to be able to
click on a hyperling and use another script to search the database for
other instances of the item. The only trouble is the result $get[name]
in the url has spaces which are then ignored by the browser query.
<?
require "config.php"; // All database details will be
included here
$page_name="test.php"; //
$query="SELECT name FROM database1 ";
$result=mysql_query($query);
echo mysql_error();
while($get = mysql_fetch_array($result))
{
echo "<a
href=http://www.myurl.com/search.php?q=$get[name]&Search=Search>$get[name]<br>";
}
?>
I've been playing around with regex and also found the code below, but
I can't figure out how to implement either. Could someone tell me how
to do this?
Thanks,
Emma
<? php
function makeUrlFriendly($input) {
// Replace spaces with underscores
$output = preg_replace("/\s/e" , "_" , $input);
// Remove non-word characters
$output = preg_replace("/\W/e" , "" , $output);
return $output;
}
// Example useage:
echo makeUrlFriendly("I'm some really long title !!1"); // Gives:
Im_some_really_long_title_1
echo makeUrlFriendly("Weird chars too: ??? &"); // Gives:
Weird_chars_too__amp
?>
Navigation:
[Reply to this message]
|