-1

How I can replace "+" to "Ӯ" and "=" to "ӯ" if right or left this symbols have a letters ? I use str_replace() but it replace all that symbols. My text:

$str = "+ро ба касе = намебахшид. + метавонист мавз=и =ро гирад. 2+1=3 ва 3 = 2 + 1"

I must get result so:

Ӯро ба касе ӯ намебахшид. Ӯ метавонист мавзӯи ӯро гирад. 2+1=3 ва 3 = 2 + 1

When I use str_replace() I get result:

str_replace(array("+","="), array("Ӯ", "ӯ"), $str);

Ӯро ба касе ӯ намебахшид. Ӯ метавонист мавзӯи ӯро гирад. 2Ӯ1ӯ3 ва 3 ӯ 2 Ӯ 1

Andreas Hunter
  • 4,504
  • 11
  • 65
  • 125
  • Yes, `str_replace` will replace all occurrences. You'll need to work with something like `preg_replace` to do more complicated string replacements. – ceejayoz Oct 18 '17 at 13:58
  • I use this `$re11='/=(?=[А-ЯҲҚӮҒӢҶ])|(?<=[А-ЯҲҚӮҒӢҶ])=/'; $re12= '/=(?=[а-яҳқӯғӣҷ])|(?<=[а-яҳқӯғӣҷ])=/'; $result = preg_replace($re11,'Ӯ', $str); $result = preg_replace($re12,'ӯ', $result);` but it not worked. @ceejayoz – Andreas Hunter Oct 18 '17 at 14:01

2 Answers2

2

Edit using unicode properties:

$str = "+ро ба касе = намебахшид. + = метавонист мавз=и =ро = гирад. 2+0=3 ва 3 = 2 + -1=+";
$str = preg_replace(array('/\+(?=\s*+[+=]?\s*\p{L})/','/=(?=\s*+[+=]?\s*\p{L})/'), array('Ӯ','ӯ'), $str);

echo $str,"\n";

Explanation:

(?=         : lookahead, make sure we have after the sign + or =
   \s*+     : 0 or more spaces, the most we can find
   [+=]?    : + or =, optional
   \s*      : 0 or more spaces, not greedy
   \p{L}    : a letter in any language
)           : end lookahead

Output:

Ӯро ба касе ӯ намебахшид. Ӯ ӯ метавонист мавзӯи ӯро ӯ гирад. 2+0=3 ва 3 = 2 + -1=+
Toto
  • 89,455
  • 62
  • 89
  • 125
  • Is this the best way to solve this problem? Or you can still optimize the code for large texts? @Toto – Andreas Hunter Oct 18 '17 at 15:17
  • You code also work incorrect when my text: +ро ба касе = намебахшид. + = метавонист мавз=и =ро = гирад. 2+0=3 ва 3 = 2 + -1=+ – Andreas Hunter Oct 18 '17 at 15:22
  • Output wrong. It must be "Ӯро ба касе ӯ намебахшид. `Ӯ` ӯ метавонист мавзӯи ӯро ӯ гирад. 2+0=3 ва 3 = 2 + -1=+" – Andreas Hunter Oct 18 '17 at 15:29
  • What happens if the symbols "=" and "+" next to each other are different symbols such as ! ? ; _ ( I will add everything to the regular expression yes? – Andreas Hunter Oct 18 '17 at 15:35
  • @Otabek: Right, but it's very hard to distinguish `+ = метавонист ` and `2 1 + = 3`. The best way is to write a parser. – Toto Oct 18 '17 at 15:38
  • @Otabek: see my new attempt – Toto Oct 18 '17 at 15:42
  • Good! But Is it impossible to express this rule in a regular expression? If the left or the right of the sigils "+" and "=" there is a letter or space and letters then replace those symbols. Your code replace `"касе = 1 намебахшид"` to `"касе = 1 намебахшид"` but will be `"касе ӯ 1 намебахшид"` – Andreas Hunter Oct 18 '17 at 15:46
  • @Otabek:How then, make difference between `= 1 намебахшид` and `2+0=3 ва`? The regex will become unreadable. – Toto Oct 18 '17 at 15:52
  • All the same, you need to write a parser, right? But I never wrote a parser. I have no idea about the parser. Thank you for helping you! – Andreas Hunter Oct 18 '17 at 16:02
0

I will use preg_replace_callback to do that:

$corr = ['+' => 'Ӯ', '=' => 'ӯ'];

$str = preg_replace_callback('~(?:[+=]\s*)+(?=\pL)|\pL\s*\K(?:[+=]\s*)+~u', function ($m) use ($corr) {
    return strtr($m[0], $corr);
}, $str);
Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125