Andreas Hunter
Andreas Hunter

Reputation: 5024

How to replace words with special symbol (.) in string?

I have sample string:

$string = "муқ. - муқоиса муқ. муқ.шавад муқ томуқ.";

I try with this my code:

$result = preg_replace("/\b(муқ\.?)\b/u", 'repl', $string);
echo "$result";

Result: repl. - муқоиса repl. replшавад repl томуқ.

Needed result: repl - муқоиса repl муқ.шавад муқ томуқ.

Here I can't replace word with "." ended symbol!

Upvotes: 2

Views: 85

Answers (3)

Gabrielle-M
Gabrielle-M

Reputation: 1077

You can use the str_replace function.

str_replace(".", "your word", "your string");

Upvotes: -1

Toto
Toto

Reputation: 91488

Use a negative lookahead:

$result = preg_replace("/\bмуқ\.(?!\w)/u", 'repl', $string);

Upvotes: 1

user70960
user70960

Reputation: 326

Try this:

$result = preg_replace("/\bмуқ\.\B/u", "repl", $string);

The shared link: https://regex101.com/r/zPXOtP/1

Upvotes: 4

Related Questions