1

I have a string for example : I am a boy

I want to show this on my url for example in this way : index.php?string=I-am-a-boy

My program :

            $title = "I am a boy";

            $number_wrds = str_word_count($title);
            if($number_wrds > 1){
            $url = str_replace(' ','-',$title);
            }else{
            $url = $title;
            }

What if I have a string : Destination - Silicon Valley

If I implement the same logic my url will be : index.php?string=Destination---Silicon-Valley

But I want to show only 1 hyphen.

I want to show a hyphen instead of a plus sign..

url_encode() will eventually insert plus symbols.. So it's not helping here.

Now if I use minus symbol then if the actual string is Destination - Silicon Valley, then the url will look like Destination-Silicon-Valley and not Destination---Silicon-Valley

Check this stackoverflow question title and the url. You will know what I am saying.

Check this

Community
  • 1
  • 1
nick
  • 333
  • 2
  • 9

4 Answers4

2

Use urlencode() to send strings along with an url:

$url = 'http://your.server.com/?string=' . urlencode($string);

In comments you told, that you don't want urlencode, you'll just replace spaces by - characters.

First, you should "just do it", the if conditional and str_word_count() is just overhead. Basically your example should look like this:

$title = "I am a boy";
$url = str_replace(' ','-', $title);

That's it.

Further you told that this would make problems if the original string already contains a -. I would use preg_replace() instead of str_replace() to solve that problem. Like this:

$string = 'Destination - Silicon Valley';
// replace spaces by hyphen and
// group multiple hyphens into a single one
$string = preg_replace('/[ -]+/', '-', $string);
echo $string; // Destination-Silicon-Valley
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • It's perfect.. Now since I am including this within an anchor tag I would have to get the url on the other php page.. On that page how do I get the original string `$string = 'Destination - Silicon Valley';` from the `preg_replace` part just like the `url_decode()` so that I can do a database query checking it's existence.? – nick Apr 13 '14 at 13:44
  • That's not possible because an irreversible change has been applied to the string. The information about the amount of whitespace and the occurrences of hyphens in the original string gets lost. Check the first paragraph of my answer. What you are searching for is urlencoding. While it is able to escape "problematic" characters, urlencoding is a reversible operation. – hek2mgl Apr 13 '14 at 13:46
  • hmm so that's a flaw here.. but ok your answer helped.! – nick Apr 13 '14 at 13:48
0

Use preg_replace instead:

$url = preg_replace('/\s+/', '-', $title);

\s+ means "any whitespace character (\t\r\n\f (space, tab, line feed, newline)).

h2ooooooo
  • 39,111
  • 8
  • 68
  • 102
0

use urlencode:

<?php
$s = "i am a boy";
echo urlencode($s);
$s = "Destination - Silicon Valley";
echo urlencode($s);
?>

return:

i+am+a+boy
Destination+-+Silicon+Valley

and urldecode:

<?php
$s = "i+am+a+boy";
echo urldecode($s)."\n";
$s = "Destination+-+Silicon Valley";
echo urldecode($s);
?>

return:

i am a boy
Destination - Silicon Valley
ChoiZ
  • 747
  • 1
  • 5
  • 23
  • adding urlencode will introduce plus sign in between.. I want a minus sign.. and if I introduce minus sign then it would look like this "Destination---Silicon-Valley"... I want to show this like this Destination-Silicon-Valley.. Stackoverflow's url are like this. I checked it.. – nick Apr 13 '14 at 11:13
0

just use urlencode() and urldecode(). It’s for sending Data with GET in the URL.

iCode
  • 1,456
  • 1
  • 15
  • 26