wehut
V2EX  ›  PHP

请教如何转换链接中的空格为“-”符号

  •  
  •   wehut · Oct 22, 2016 · 2950 views
    This topic created in 3517 days ago, the information mentioned may be changed or developed.
    <p>观看作者“<a href="http://www.xxxxxx.com/tag/<?php $name = get_post_meta($post->ID, 'author', true); if ($name) { echo "$name";}?>"><?php $name = get_post_meta($post->ID, 'author', true); if ($name) { echo " $name ";}?></a>”的其他作品</p>

    这是 wordpress 中(自定义栏目)自定义字段的 ,$name 中输出的内容也就是 author 栏目里的内容, 最终输出的效果就是: http://www.xxxxxx.com/tag/$name 但如果 $name 内的内容 有空格的话输出的链接 就会无法访问 会出现 404 。

    比如: http://www.xxxxxx.com/tag/The Frostbite Project
    改为: http://www.xxxxxx.com/tag/The-Frostbite-Project

    现在就想解决如何把 $name 的内容中的 空格 自动由 php 替换成 “-”符号 这样就可以正常访问, 之所以不再 $name 里直接加上 - 是因为 这样调用出来前端显示不美观。

    急着解决,现在大量 404
    8 replies    2016-10-25 16:48:34 +08:00
    loveyu
        1
    loveyu  
       Oct 23, 2016 via Android
    果然大家都是懒得回答
    nfroot
        2
    nfroot  
       Oct 23, 2016
    @loveyu 既然你都这么说了 那你上吧
    shiny
        3
    shiny  
    PRO
       Oct 23, 2016
    为什么不直接用空格,直接 urlencode 就行了。
    beginor
        4
    beginor  
       Oct 23, 2016 via Android
    urlencode +1 , 或者手工将空格转换为 %20
    ids
        5
    ids  
       Oct 23, 2016 via Android
    urlencode+2
    wehut
        6
    wehut  
    OP
       Oct 24, 2016
    拜托大家,现在出现很多 404 。
    AbrahamGreyson
        7
    AbrahamGreyson  
       Oct 24, 2016   ❤️ 1
    ```php
    echo $name ? str_replace(' ', '-', $name) : null;
    ```
    wehut
        8
    wehut  
    OP
       Oct 25, 2016
    @AbrahamGreyson 真是太谢谢你了,对于我这样不会程序的人,一直苦恼了很久,太感谢了。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2310 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 51ms · UTC 16:12 · PVG 00:12 · LAX 09:12 · JFK 12:12
    ♥ Do have faith in what you're doing.