Forum Moderators: coopster
the following is my result from brower.
http://www.example.com/test%20%20test...%20test/%2044/
i try to have result like the following;
http://www.example.com/test-test-test/2044/
my code as following:
<?php
if(isset($_POST['subject'])){
$subject=$_POST['subject'];
$id=$_POST['id'];
}$subject=trim($subject);
$subject=str_ireplace("%20","-",$subject);$id=trim($id);
$id=str_ireplace("%20","",$id);?>
<html><head>
<meta http-equiv="Refresh" content="0; url=/<?php echo $subject?>/ <?php echo $id?>/">
</head><body>
Please follow <a href="/<?php echo $subject?>/ <?php echo $id?>/">link</a>!
</body></html>
[edited by: eelixduppy at 3:06 am (utc) on Aug. 27, 2007]
[edit reason] changed to example.com [/edit]
[edited by: eelixduppy at 12:25 pm (utc) on Aug. 27, 2007]
$subject=trim($subject);
$subject=str_ireplace("%20","-",$subject);$id=trim($id);
$id=str_ireplace("%20","",$id);
%20 is on URLs when you have space on your URL. You don't replace the "%20", you replace your spaces. This is how it should be:
$subject=trim($subject);
$subject=str_ireplace(" ","-",$subject);
$id=trim($id);
$id=str_ireplace(" ","",$id);
Habtom
%20 is on URLs when you have space on your URL. You don't replace the "%20", you replace your spaces. This is how it should be:$subject=trim($subject);
$subject=str_ireplace(" ","-",$subject);$id=trim($id);
$id=str_ireplace(" ","",$id);
It give me the following result after i rewrite as above;
http://www.example.com/xsd-----dd/%2056/
do you known how to get the following format;
http://www.example.com/xsd-dd/56/
[edited by: dreamcatcher at 7:41 am (utc) on Aug. 27, 2007]
[edit reason] Use example.com, thanks. [/edit]
Replace the relevant lines
$subject=trim($subject);
//The following replaces two spaces with one.
// I think at the end you get only one space no matter how many spaces you have had before.
$subject = str_replace(" ", " ", $subject);
i rewrite as following;
$subject=trim($subject);
$subject = str_replace(" ", " ", $subject);
$subject=str_replace(" ", "-", $subject);
$id=trim($id);
$id=str_ireplace(" ","",$id);
and i get the result from brower as following;
http://example.com/sds---ffxdf/%2062/
but i want the final result as following;
http://example.com/sds-ffxdf/62/
[edited by: eelixduppy at 12:24 pm (utc) on Aug. 27, 2007]
. . . I think the spaces were removed when posted.
$subject = str_replace("[Two Spaces]", "[One Space]", $subject);
My code rewrite as following;
$subject=trim($subject);
$subject = str_replace("[Two Spaces]", "[One Space]", $subject);
$id=trim($id);
$subject = str_replace("[Two Spaces]", "[One Space]", $subject);
i have the result from browser as following;
http://example.com/sd%20%20%20ssrf/%2064/
[edited by: eelixduppy at 12:24 pm (utc) on Aug. 27, 2007]
function makeURLFriendly($input){
$output = strtolower($input);
$output = preg_replace("/[^[:space:]a-z0-9]/e", "", $output);
$output = trim($output);
$output = preg_replace('/\s\s+/', '_', $output);
return $output;
}
origianally posted at:
[webmasterworld.com...]
Hab
function makeURLFriendly($input){
$output = strtolower($input);
$output = preg_replace("/[^[:space:]a-z0-9]/e", "", $output);
$output = trim($output);
$output = preg_replace('/\s\s+/', '_', $output);
return $output;
}//function makeURLFriendly($input){
echo $input = ' My Directory & Stuff ';
echo '<br />';
echo makeURLFriendly($input); // my_directory_stuff
Your one seems work, but it just got a litter problem for now.
when my input is the "$input = ' My Directory & Stuff '", it give me the result is "my directory_stuff". not the "my_directory_stuff".
and i want to ask you what does the following two lines do?
1) $output = preg_replace("/[^[:space:]a-z0-9]/e", "", $output);
2) $output = preg_replace('/\s\s+/', '_', $output);
function makeURLFriendly($input){
$output = strtolower($input);
$output = preg_replace(" ", "_", $output);
$output = trim($output);
$output = preg_replace('/\s\s+/', '_', $output);
return $output;
}
The compiler complains give me the following massage;
Warning: preg_replace() [function.preg-replace]: Empty regular expression
for the line;
"$output = preg_replace(" ", "_", $output)"
1) replace all the characters(without [a-z]) from the string by using space.
2) trim($string)
3) replace the spaces by "_".
But the 1), i think using regular expression would be better. But i forget how to use the regular expression, i am going to read then now.