dsb0003795 2014-11-17 10:38
浏览 74
已采纳

如何在PHP中的任何标点符号和标记之间添加空格? [关闭]

I want to add a space in between each punctuation symbol and token, so that the text looks something like this:

 " in dreams " might keep you awake at night , but not because of its creepy imagery , bizarre visual style or story about a clairvoyant madman who lures young girls to their untimely deaths . 
no , the source of potential sleeplessness here lies within the movie's brutally squandered potential , the least of which is an admittedly nifty premise - even by tired serial killer genre standards . 
the big letdown , however , comes upon the realization that this 100-minute head-scratcher was masterminded by neil jordan , the man behind " the crying game . " 
he's no stranger to cinematic weirdness , but this nutty nonsense really pushes the envelope . 
things start out strong enough , with cinematographer darius khondji's stunning camera work guiding viewers into the bowels of a underwater ghost town during a creepy prologue that establishes a notably grim tone right off the bat . 
this eerie opulence remains a dazzling display of showmanship throughout the entire film - there's even something macabre about the way khondji photographs a rustic , seemingly innocent new england autumn - but if there ever was a film that didn't deserve so good a polish , it's this one .

Note that the input string could be, for example, "This is a simple - straightforward P.H.P. Program! Isn't it?"

The output should be: " This is a simple - straightforward P . H . P . Program ! Isn ' t it ? "

Punctuation characters in this case are anything that is not a letter or a number.

  • 写回答

1条回答 默认 最新

  • douhui8025 2014-11-17 10:43
    关注

    I suggest not to use "hungry" regular expression engine in your case, simple string replace will do the job:

    <?php
    $input_string = "bla bla,bla.another bla;";
    //you can extend this array as you wish
    $punctoations = array(".",",",":",";");  
    //your replacements, same order as above array    
    $replacements = array (" . "," , "," : "," ; "); 
    echo str_replace($punctoations, $replacements, $input_string);
    ?>
    

    See how more info in PHP documentation.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办
  • ¥15 vue2登录调用后端接口如何实现