【问题标题】:how to have multiple replace on the same line如何在同一行上进行多个替换
【发布时间】:2020-04-24 02:45:00
【问题描述】:

我是编程新手,你可以帮我解决下面的问题:

我在下面的行中将空格“”替换为破折号 (-)

$url = addslashes( 'mysite.com/pages/'.strtolower(str_replace(' ','-', $q)) ).'.html';

但我如何将它与代码放在同一行以也替换问号(?)和与号(&)为空“”

我尝试了下面的方法,但没有成功,我错过了一些东西

$url = addslashes( 'mysite.com/pages/'.strtolower(str_replace('?','',(str_replace('&','',(str_replace(' ','-', $q)))))) ).'.html';

【问题讨论】:

标签: php replace str-replace


【解决方案1】:

您可以使用此代码将空格替换为 "-" 。

'The text you want to change'.toLowerCase().split(" ").join("-");

var a = 'The text you want to change';
a = a.toLowerCase().split(" ").join("-");

【讨论】:

    【解决方案2】:

    它会帮助你

    $your_string = 'you_input_string';
    $search = [" ", "?", "&"];
    $replace = ["-", "", ""];
    echo str_replace($search, $replace, $your_string);
    

    【讨论】:

    • 我把它放在下面但它没有工作 $url = addlashes( 'mysite.com/page/'.strtolower(str_replace(array('?', ''),(array( '&', ''), array(' ','-', $q))))))).'.html';
    • 你能告诉我你为什么使用addlashes()函数吗?你的输入字符串是什么,你想要什么样的输出?
    • 它是一个搜索站点,例如当它被搜索(快车)时,搜索在我的站点地图上保存为hotbusca.com/paginas/fast-car.html 但是,如果搜索包含符号(& 或?)站点地图链接不起作用。我的站点地图链接:hotbusca.com/sitemap.xml?id=2
    • 当您从输入框中获取搜索值时,将值存储在变量中,然后应用 str_replace 方法,然后将其附加到您的站点 URL
    猜你喜欢
    • 2020-07-30
    • 2021-11-07
    • 1970-01-01
    • 2020-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-04
    • 2011-02-07
    相关资源
    最近更新 更多