6

I'm using PHP smarty to declare a link :

<{foreach item=list key=num from=$product}> 
    <li><a href="<{$config.weburl}>/<{$list.somename}>"><{$list.somename}></a></li>
<{/foreach}>`

and the resulting link when mouseover is :

"http:/domain/some name"

I need to str_replace the space char (some name) with an underscore (some_name), how to do that?? like result below :

"http:/domain/some_name"

I'm using the following code but it does not work. How to use str_replace with an array in html?

<a href="<{$config.weburl}>/'.str_replace(array(' ','%'),array('_','-'),<{$list.somename}>).'">
Synchro
  • 35,538
  • 15
  • 81
  • 104
prieku
  • 145
  • 2
  • 2
  • 8

2 Answers2

16

You can use PHP functions as Smarty modifiers, but Smarty has a built-in replace modifier. Use it like this:

<{foreach item=list key=num from=$product}> 
    <li><a href="<{$config.weburl}>/<{$list.somename|replace:' ':'_'}>"><{$list.somename}></a></li>
<{/foreach}>
Synchro
  • 35,538
  • 15
  • 81
  • 104
0

Try :

str_replace(" ", "_", $config.weburl)

This means : replace the character " " (space) by "_" in $config.weburl.

This would replace all spaces with underscores in PHP. Try { str_replace(" ", "_", $config.weburl) } to echo it with Smarty PHP, but as I don't use it, I'm not sure this will work.

Here's some docs : http://php.net//manual/fr/function.str-replace.php

enguerranws
  • 8,087
  • 8
  • 49
  • 97