2

I found this solution: here but its in javascript, I try to do it with PHP and I got a warning.

Here is my code:

$dynamicstring = 'שָׁמַיִם';
$newstring2 = preg_replace('[\u0591-\u05C7]', '', $dynamicstring);

The warning I have got:

Warning: preg_replace(): Compilation failed: PCRE does not support \L, \l, \N{name}, \U, or \u at offset 1 in

What is it mean? so if it not support what can I use?

Edit: I was able to do this by the following code:

preg_replace('/[^\w$\x-\x]+/u', '', $dynamicstring);

But this code remove spaces between words in the string, how to fix that?

Second Edit: the solution is to do this:

preg_replace('/[^\w$\x-s-\x]+/u', '', $dynamicstring);

I added -s- between the \xs

John
  • 41
  • 8
  • 1
    Try https://stackoverflow.com/questions/26458654/regular-expressions-for-a-range-of-unicode-points-php? – user202729 Sep 05 '18 at 04:58
  • @user3783243 I try the samples and here what I do: ``$newstring2 = preg_replace('/[^\w$\x{0591}-\x{05C7}]+/u', '', $dynamicstring);``, nothing happens – John Sep 05 '18 at 05:14
  • What should happen, what is a hebrew vowel? The `\w` probably contradicts your other statements, I get no errors though and have no knowledge of hebrew so you really nned to add more to the question. https://3v4l.org/oLlVD – user3783243 Sep 05 '18 at 05:17
  • @user3783243 please see edit, the code I wrote work fine and remove the hebrew vowels but it remove spaces between words in the string too, how to fix that? – John Sep 05 '18 at 05:20

1 Answers1

3

These are non-spacing marks, so (assuming the string is in UTF-8) they can be stripped using the Unicode character property syntax:

<?php

$dynamicstring = 'שָׁמַיִם';
$newstring2 = preg_replace('/\p{Mn}/u', '', $dynamicstring);
var_dump($newstring2); // results in string(8) "שמים"

Demo: https://ideone.com/KpsZM3

weirdan
  • 2,499
  • 23
  • 27
  • Thanks but I solved the problem with another regex - I have a question, what is it mean ``{Mn}``? – John Sep 05 '18 at 17:05
  • '**M**ark, **n**on-spacing'. There's a table with all property designators on [this page](http://php.net/manual/en/regexp.reference.unicode.php) (also linked in the answer body). – weirdan Sep 05 '18 at 18:52