1

How to add <br> before Chinese character if Chinese wording is combined with normal text.

<?php

$string = 'Hello World 自立合作社';

/*
this is what I tried:
preg_match('/\\p{Han}/u', $string, $matches);
print_r($matches)
*/

?>

Output:

Hello World</br>自立合作社
Amal Murali
  • 75,622
  • 18
  • 128
  • 150
rusly
  • 1,504
  • 6
  • 28
  • 62

2 Answers2

1

This is surely not the best solution, but one approach would be to match a string of ASCII characters via [\x00-\x7F]+ followed by a non-ASCII sequence (same pattern negated with ^). It does not target Chinese specifically, but that is tricky owing to the varied ranges of Chinese Unicode characters.

$string = 'Hello World 自立合作社';
// Capture ASCII sequence into $1 and non-ASCII into $2
echo preg_replace('/([\x00-\x7F]+)([^\x00-\x7F]+)/', '$1<br/>$2', $string);

// Prints:
// Hello World 
// 自立合作社

http://codepad.viper-7.com/1kqpOx

Actually, here's an improved version that does specifically target Chinese characters via \p{Han}. The $2 capture also includes \s for whitespace.

// This matches any non-Chinese in $1 followed by Chinese (and whitespace) in $2
echo preg_replace('/([^\p{Han}]+)([\p{Han}\s]+)/', '$1<br/>$2', $string);
Community
  • 1
  • 1
Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
1

I am no Chinese but I hope this helps.

As you are using php you can use preg_replace with look ahead construct. It will replace all white space characters (those before chinese character) with a <br>.

$string = 'Hello World 自立合作社';
$pattern = "/\\p{Z}(?=\\p{Han})/ui"; // matches a white space before Chinese character. 

$brStr = preg_replace($pattern, "<br>", $string);
echo $brStr;

I don't know will \p{Han} match all Chinese characters so checkout more info on unicode characters here

May be this one will help too

Hope this helps. Good luck! ;)

Community
  • 1
  • 1
user1759572
  • 683
  • 4
  • 11