简体   繁体   中英

Add '<br>' before chinese character

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>自立合作社

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);

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! ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM