2

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!

Andreas Hunter
  • 4,504
  • 11
  • 65
  • 125
  • `\b` doesn't work good with unicode characters.. You can try [a proposed workaround here](https://stackoverflow.com/a/45985701/7393478) – Kaddath May 02 '18 at 07:41

3 Answers3

4

Try this:

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

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

user70960
  • 326
  • 1
  • 16
1

Use a negative lookahead:

$result = preg_replace("/\bмуқ\.(?!\w)/u", 'repl', $string);
Toto
  • 89,455
  • 62
  • 89
  • 125
-1

You can use the str_replace function.

str_replace(".", "your word", "your string");
user70960
  • 326
  • 1
  • 16
Gabrielle-M
  • 1,037
  • 4
  • 17
  • 39