【问题标题】:Change Spaces (%20) to -将空格 (%20) 更改为 -
【发布时间】:2013-01-09 09:36:30
【问题描述】:

嘿,我现在有我的网站 URL 链接到这里:

 <div id="blog_posts_container">
    <h3><a href="<?php echo (isset($row_getDisplay['post_id']) ? $row_getDisplay['post_id'] : ''); ?>_<?php echo (isset($row_getDisplay['title']) ? $row_getDisplay['title'] : ''); ?>.html"<?php echo (isset($row_getDisplay['title']) ? $row_getDisplay['title'] : ''); ?></a></h3>

在我的 .htaccess 中我有:

RewriteRule ^([^_]*)_([^_]*).html$ post.php?post_id=$1&title=$2 [L]

如何将我的网址从“86_I%20am,%20who%20I%20am.html”获取到“86_I-am-who-I-am.html”

编辑: 我做到了,我得到它来替换特殊字符,例如:; / 等,但现在它又有了空格。这是我的代码:

<h3><a href="<?php echo (isset($row_getDisplay['post_id']) ? $row_getDisplay['post_id'] : ''); ?>_<?php echo str_replace(array(':', '\\', '/', '*'), ' ', urldecode($row_getDisplay['title'])); ?>.html" ><?php echo (isset($row_getDisplay['title']) ? $row_getDisplay['title'] : ''); ?></a></h3>

【问题讨论】:

  • 你为什么一开始会得到这么奇怪的网址?最初生成好的网址怎么样,而不是试图修复垃圾?
  • @zerkms 你是什么意思

标签: php


【解决方案1】:

使用str_replace函数替换空格来——像这样:

$url = str_replace(' ', '-', urldecode($row_getDisplay['title']));

见 PHP 手册str_replace

【讨论】:

  • 您需要执行 urldecode($row_getDisplay['title']) 以便您的解决方案正常工作。
  • @m4t1t0 对不起,我的英文不好,没看懂,请帮我修改,谢谢:)
  • @Robdogga55 输出前替换标题
  • @m4t1t0 谢谢,我明白了。
  • 谢谢,我也知道了。还有一件事,我将如何删除 URL 中的所有特殊字符(例如!:等)
【解决方案2】:
 urldecode($row_getDisplay['title'])

%20 是单词之间的空格

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-30
    • 1970-01-01
    • 2012-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多