如何在URL中将空格替换为连字符

I have some data that is inputted into to a database with spaces. e.g. first and last names. I then need to call that data from the database and display it as a link so i have a friendly URL. I am not sure if I should do this with mod-rewrite or php. What is the best solution?

A solution like below doesn't seem to work

str_replace('- ','-',$url);

echo "<p><span class=\"barting\">"."<a href=$url=\"jobs/".$row['jobid']."/".$row['title']."\">".$row['title']."</a></span>";

echo $url

Thanks for help in advance

4个回答



尝试 urlencode() </ p>

urlencode($ url)</ code> </ p>
</ div>

展开原文

原文

Try urlencode()

urlencode($url)



你写了</ p>

str_replace(' - ',' - ',$ url ); </ code> </ p>

但此代码不替换空格,以替换应使用的空格:</ p>

$ url = str_replace('',' - ',$ url); </ code> </ p>

或者您可以使用 urlencode </ p>
</ div>

展开原文

原文

You wrote

str_replace('- ','-',$url);

But this code don't replace white spaces, to replace white spaces you should use:

$url = str_replace(' ','-',$url);

or you can use urlencode



您应该使用urlencode(),它会将空格转换为%20。 但是如果你想做你正在做的事情,你必须记住str_replace RETURNS替换的字符串,它不会修改你传入的var。</ p>

所以你需要
$ url = str_replace('',' - ',$ url); </ p>
</ div>

展开原文

原文

You should probably use urlencode(), which will turn the spaces into %20. But if you want to do what you're doing, you have to remember that str_replace RETURNS the replaced string, it does not modify the var you pass in.

So you need $url = str_replace(' ', '-', $url);



不需要重写URL。 听起来你的目标是简单地构建一个看起来像这样的URL:</ p>

  http://www.example.com/firstname-lastname/jobs/1234567/sometitle

</ code> </ pre>

要仅用$ url中的连字符替换空格,请尝试</ p>

  str_replace(“”,“ - ”  ,$ url); 
</ code> </ pre>
</ div>

展开原文

原文

A URL rewrite is not necessary. It sounds like your goal is to simply build a URL that looks something like this:

http://www.example.com/firstname-lastname/jobs/1234567/sometitle

To replace only spaces with hyphens in $url, try

str_replace(" ", "-", $url);

dqd72925
dqd72925 是的,这是我想要的,但我如何将其实现到我的代码中? 如何让它影响我的代码。
大约 10 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐