Replace two "words" in a string using str_replace

Borja

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) ?

With javascript i used:

string.replace(/word-|-/g," ");

so i want replace this two words:

 /word-
 -/

But how is possible in php ? i tried to use also

 preg_replace(array('/word-','-/')," ",$string);

but nothing :( i hope you can help me

chris85

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:

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Trying to replace a () in a string in R using str_replace

From Dev

str_replace replace only match words

From Dev

Replace multiple words in a string ln java like php str_replace

From Dev

Two Times str_replace

From Dev

str_replace php a string

From Dev

Using a function in str_replace

From Dev

Ruby: How to replace the words in string by two arrays?

From Dev

replace two separate words in a matched string sed

From Dev

PHP - str_replace returns original string

From Dev

str_replace() removes numeric value with / in a string?

From Dev

str_replace from a particular position in a string

From Dev

multiple str_replace to one string? PHP

From Dev

str_replace() removes numeric value with / in a string?

From Dev

Using double quotes on str_replace

From Dev

PHP str_replace tags with using arrays

From Dev

Using str_replace to alter the_title()

From Dev

Foreach loop using multiple str_replace

From Dev

failed when using str_replace

From Dev

Find and replace/remove two words from a string using regex c#

From Dev

loads of spaces when echoing php string after using str_replace

From Dev

how to add double quotation character in PHP string using str_replace

From Dev

pandas string replace TypeError - replace words using pandas df column

From Dev

Replace all the first character of words in a string using preg_replace()

From Dev

Replace only the first character of a string with str_replace?

From Dev

str_replace() doesn't replace needle in a Chinese string

From Dev

PHP str_replace - Filter and remove 2 words from array?

From Dev

python : replace words in string

From Dev

Replace words in a a string randomly

From Dev

Replace words in a a string randomly

Related Related

HotTag

Archive