In PHP
using str_replace
how is possible replace two words in a string with only one word (in my case is space and not word) ?
在PHP中,使用str_replace如何可能只用一个单词替换字符串中的两个单词(在我的例子中是空格而不是单词)?
With javascript i used:
使用javascript我使用:
string.replace(/word-|-/g," ");
so i want replace this two words:
所以我想替换这两个词
/word-
-/
But how is possible in php ? i tried to use also
但是如何在php中实现呢?我也试着使用
preg_replace(array('/word-','-/')," ",$string);
but nothing :( i hope you can help me
我希望你能帮助我
1 个解决方案
#1
3
This:
这样的:
str_replace(array('/word-', '-/'), ' ', $string);
should work for you. The /
s in the JS example though are delimiters showing where the regex starts and ends. So in the PHP preg_replace
you'd need to do:
应该为你工作。JS示例中的/s是分隔符,显示regex的开始和结束。在PHP preg_replace中,你需要:
preg_replace(array('~/word-~','~-/~')," ",$string);
or maybe simpler:
或者更简单:
preg_replace('~(/word-|-/)~'), " ",$string);
Note the above matches what you've stated but not what your JS is doing.
请注意,上面的内容与您所陈述的内容相匹配,而不是您的JS正在做什么。
#1
3
This:
这样的:
str_replace(array('/word-', '-/'), ' ', $string);
should work for you. The /
s in the JS example though are delimiters showing where the regex starts and ends. So in the PHP preg_replace
you'd need to do:
应该为你工作。JS示例中的/s是分隔符,显示regex的开始和结束。在PHP preg_replace中,你需要:
preg_replace(array('~/word-~','~-/~')," ",$string);
or maybe simpler:
或者更简单:
preg_replace('~(/word-|-/)~'), " ",$string);
Note the above matches what you've stated but not what your JS is doing.
请注意,上面的内容与您所陈述的内容相匹配,而不是您的JS正在做什么。