phpregexstringeregiereg

PHP: make a string clickable


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?


Solution

  • With this regex you can split your characters.

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

    the result is array of three character.