校对(修复标点符号、空格、大写字母)使用 php 的生物页面文本


Proofing (fixing punctuation, spaces, uppercase letters) a bio page text with php

我需要帮助来修剪用户在其个人资料中提交的生物信息。有时存在校对问题:"这是迈克的简历,无缘无故地大写!问号和单词之间没有空格?问题mar也应该有一个空格.在停止标志之后也应该有大写,这个逗号之间应该有空格,还有,这个,一个"

这是我的想法:

首先我会修剪$bio变量

$bio = trim($bio); 

然后我会在标点符号后添加空格 - 很确定这是不正确的,因为它用逗号替换每个标点符号类型。

$bio = str_replace(array(",","!","?"),", ", $bio); 

然后我会把所有的字母都变成小写;这行不通,因为我需要在$bio变量中保留句子第一个单词的第一个字母的大写。

$bio = strtolower($bio); 

最后我会大写第一个; 但我需要一种方法来大写每个单词的首字母,用 !? - 或停车标志,你知道...逗号除外。

$bio = strtoupper($bio); 

希望你能帮到你

我必须警告你,它看起来没有希望。

无论如何,你也许可以用一系列正则表达式做一些事情:

 // This replaces , . ! ? (if NOT followed by a space) with the same ('1),
 // followed by a space, followed by whatever followed it before ('2).
 // Note that . and ? are special characters for regexes, so we have to
 // escape them with a "'".
 $bio = preg_replace('#([,'.!'?])('S)#ms', ''1 '2', $bio);
 // Then replace all extra spaces: any sequence of 2 or more spaces is
 // replaced by one space.
 $bio = preg_replace('# {2,}#ms', ' ', $bio);
 // Then ., !, and ? followed by lowercase should uppercase it
 // We take the full monty, ". m" and uppercase it all. Since the uppercase
 // of ". " remains ". ", we keep things simpler.
 $bio = preg_replace('#['.!'?] [a-z])#ms', 'strtoupper("'1")', $bio);
 // Then replace ALL CAPS words with lowerspace equivalent.
 // Doesn't seem a really good idea though: "I am Mike, I worked with NASA"
 // and NASA becomes nasa?
 $bio = preg_replace('# ([A-Z][A-Z]+)#mse', 'strtolower(" '1")', $bio);

这样,你的句子就变成了:

这是迈克的简历,无缘无故地大写!问号和单词之间没有空格吗?此外,问题 mar 应该有一个空格。此外,在停车标志之后应该有大写,这个逗号之间应该有空格,还有一个