2

I want to replace any space in $string with an underline

but NOT if the space is surrounded by [a-z]

$string = '@@ @@ English something else';

I tried this but it replaces "@@ @@" to "@_@"

$string = preg_replace('/[^a-z]\s[^a-z]/ui', '_' , $string);

I want the output to look like this

@@_@@_English something else

sorry i don't know how to use code in comment, so just add a reply

tested the 2nd and 3rd pattern also work

i understand the 2nd one, but the 3rd one a bit hard for me LOL

i need to go check https://www.regular-expressions.info/

$string = '英文名 中文名 Peter Wong,John Tam,Sam Hu';
$string = preg_replace('/(?<![a-z]) | (?![a-z])/i', ',', $string);

output is

英文名,中文名,Peter Wong,John Tam,Sam Hu

because CJK Name is no space between each char, so this solved the big problem for me

worked like a charm ;)

Thankyou @mickmackusa

ChrisF
  • 134,786
  • 31
  • 255
  • 325
kenken9999
  • 765
  • 1
  • 6
  • 14

2 Answers2

2

I don't think you need to include the u unicode flag for this task.

Simply disqualify the undesirable spaces with (*SKIP)(*FAIL) then replace all qualifying spaces.

Code: (Demo) (Pattern Demo)

$string = '英文 中文 English something else';
echo preg_replace('/[a-z] [a-z](*SKIP)(*FAIL)| /i','_',$string);

Output:

英文_中文_English something else

Pattern Explanation:

[a-z] [a-z](*SKIP)(*FAIL) <-- this means match and discard the letter-space-letter occurrences
| <-- this mean "OR"
<-- this will match every non-discarded space in the string (only these will be replaced


Two alternatives with lookarounds will also work (and faster): (Demo)

/(?<![a-z]) | (?![a-z])/i

Fastest of all will be the \B (negated version of the word boundary metacharacter): (Demo)

/\B | \B/
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
0

I'm not sure what language that is but I found this: Use regular expression to match ANY Chinese character in utf-8 encoding

Your regular expression needs to contain (all) Chinese characters, which can be expressed as:

\p{Han}

Here is PHP's documentation on Unicode characters and a list of supported scripts: http://php.net/manual/en/regexp.reference.unicode.php

ihodonald
  • 745
  • 1
  • 12
  • 27