如何使用php或jquery用href替换空间

时间:2015-02-07 09:54:37

标签: php url href

我想用 - 在php smarty中的标签 - > href属性替换空格;

[密钥]是动态的

是什么方式? <a href="key.php?c={$obj->a[key]}">{$obj->a[key]}</a>

2 个答案:

答案 0 :(得分:0)

这可能是你的答案

<a href="key.php?c={$obj->a[key]|replace:' ':'-'}">{$obj-a[key]|replace:' ':'-'}</a>

http://www.smarty.net/docsv2/en/language.modifier.replace.tpl

答案 1 :(得分:0)

you should try this
  

str_replace()是一个php函数,用于替换之间的字符   句子。函数中基本上有三个参数传递。   第一个参数:搜索角色,第二个参数:替换角色,   第三个论点:句子。

<?php
$str='home and car';
echo '<a href="key.php?c='.str_replace(' ','-',$str).'">'.str_replace(' ','-',$str).'</a>';
?>

输出

enter image description here

jquery代码

  

g是一个正则表达式代码,它替换了字符串之间的所有空格。

<script>
$("a").each(function() {
    var text = $(this).text();
    text = text.replace(/ /g, "-");
    $(this).prop('href',text);
    $(this).text(text);
});
</script>

输出

<a href="home-and-car">home-and-car</a>