1

For a Mandarin learning-tool I would like to create links for each chinese "character" within a word. For example I have the chinese word "自行车" (bicycle). Then I would like to make each of the three characters "clickable".

$word = '自行车';

And the output should be:

$output = "<a href='?char=自'>自</a> 
<a href='?char=行'>行</a> 
<a href='?char=车'>车</a>";

Does anyone have an idea how to do this?

LF00
  • 27,015
  • 29
  • 156
  • 295
D. Studer
  • 1,711
  • 1
  • 16
  • 35

3 Answers3

2

With this regex you can split your characters.

preg_split('//u', "自行车", null, PREG_SPLIT_NO_EMPTY);

the result is array of three character.

ttrasn
  • 4,322
  • 4
  • 26
  • 43
1

You can use preg_replace to replace the chinese char with regular expression /(\p{Han})/u then replace it with what your need.

preg_replace("/(\p{Han})/u","<a href='?char=$1'>$1</a>",'ss自行车ss');

output:

ss<a href='?char=自'>自</a><a href='?char=行'>行</a><a href='?char=车'>车</a>ss

Refer to Php - regular expression to check if the string has chinese chars

LF00
  • 27,015
  • 29
  • 156
  • 295
0

You can extract chars in a for loop and generate anchor tags.

<a href='<link?char=<extracted char>'><extracted char></a>