在php中添加/修复标点符号

在php中添加/修复标点符号,php,punctuation,Php,Punctuation,我正在慢慢完善PHP中的标点符号修复函数,该函数用于清理用户输入。该函数当前在标点符号后添加空格,删除标点符号前的空格,并将每个句子的第一个单词大写。我已经看到一些人在寻找类似的功能,所以我很高兴分享到目前为止我所拥有的。它非常接近我想要的位置,但是,当它在逗号后添加空格时,应该避免在逗号位于数字(如1000)内时这样做。有人能建议修改代码以忽略数字内的逗号的最快方法吗?也许有办法缩短我所拥有的,但仍然达到相同的结果?谢谢你的时间 function format_punc($string){

我正在慢慢完善PHP中的标点符号修复函数,该函数用于清理用户输入。该函数当前在标点符号后添加空格,删除标点符号前的空格,并将每个句子的第一个单词大写。我已经看到一些人在寻找类似的功能,所以我很高兴分享到目前为止我所拥有的。它非常接近我想要的位置,但是,当它在逗号后添加空格时,应该避免在逗号位于数字(如1000)内时这样做。有人能建议修改代码以忽略数字内的逗号的最快方法吗?也许有办法缩短我所拥有的,但仍然达到相同的结果?谢谢你的时间

function format_punc($string){
    $punctuation = ',.;:';
    $string = str_replace(' ?', '?', str_replace(' .', '.', str_replace(' ,', ',', preg_replace('/(['.$punctuation.'])[\s]*/', '\1 ', $string))));
    $string = trim(preg_replace('/[[:space:]]+/', ' ', preg_replace('/([\.!\?]\s+|\A)(\w)/e', '"$1" . strtoupper("$2")', $string)));
    if($string[strlen($string)-1]==','){
        $string = substr($string, 0, -1).'.';
    }
    return $string;
}

我认为regexp应该是([^0-9][.][^0-9])[\s]*


这有点复杂,但它应该能让你找到正确的方向:

<?php

// The following finds all commas in $string and identifies which comma is preceded and followed by a number

$string = 'Hello, my name, is John,Doe. I have 3,425 cats.';

function strpos_r($haystack, $needle)
{
    if(strlen($needle) > strlen($haystack))
        trigger_error(sprintf("%s: length of argument 2 must be <= argument 1", __FUNCTION__), E_USER_WARNING);

    $seeks = array();
    while($seek = strrpos($haystack, $needle))
    {
        array_push($seeks, $seek);
        $haystack = substr($haystack, 0, $seek);
    }
    return $seeks;
}

var_dump($commas = strpos_r($string, ',')); // gives you the location of all commas

for ($i = 0; i <= count($commas) - 1; $i++)
{
    if (is_numeric($commas[$i] - 1) && is_numeric($commas[$i] + 1)) 
    {
      // this means the characters before and after a given comma are numeric
      // don't add space (or delete the space) here

    }
}

这是我更新的php修复标点函数。。。它现在似乎工作正常。我相信有很多方法可以压缩它,但它可以对字符串执行以下操作。。。


减少重复标点符号,例如!!到
将多个空间减少为单个空间
删除之前的任何空格,
在后面添加空格;:
在逗号后添加空格,但当它们是数字的一部分时不添加空格
在句点后添加空格,但不能在句点是数字或缩写的一部分时添加空格
删除字符串开头和结尾的空白
将句子的第一个单词大写
如果最后一个字符是逗号,请将其更改为句点

function format_punc($string){
    $punctuation = ';:';
    $spaced_punc = array(' ?', ' .', ' ,');
    $un_spaced_punc = array('?', '.', ',');
    $string = preg_replace("/([.,!?;:])+/iS","$1",$string);
    $string = preg_replace('/[[:space:]]+/', ' ', $string);
    $string = str_replace($spaced_punc, $un_spaced_punc, $string);
    $string = preg_replace('/(['.$punctuation.'])[\s]*/', '\1 ', $string);
    $string = preg_replace('/(?<!\d),|,(?!\d{3})/', ', ', $string);
    $string = preg_replace('/(\.)([[:alpha:]]{2,})/', '$1 $2', $string);
    $string = trim($string);
    $string = preg_replace('/([\.!\?]\s+|\A)(\w)/e', '"$1" . strtoupper("$2")', $string);
    if($string[strlen($string)-1]==','){
        $string = substr($string, 0, -1).'.';
    }
    return $string;
}
函数格式\u punc($string){
$标点符号=';:';
$spaced_punc=数组(“?”、“.”、“,”);
$un_spaced_punc=数组(“?”、“.”、“,”);
$string=preg_replace(“/([,!?;:])+/iS“,”$1“,$string);
$string=preg_replace('/[:space:]+/',''$string);
$string=str_replace($spaced_punc,$un_spaced_punc,$string);
$string=preg_replace('/(['.$标点符号'])[\s]*/','\1',$string);

$string=preg_replace('/(?你如何确定逗号是一个千位分隔符,或者你处理的是一个数字的枚举?这就是我发布这篇文章的原因…我希望有人能帮助改进这个功能,判断它是否处理数字。一些国家使用不同的1000位分隔符,也许可以从设置中推断出来…(只是说)谢谢,这很接近…但它会导致一个空格,如“String,w ord.2000”您如何保护电子邮件地址或URL中的标点符号?对于我的使用,在字符串传递到此函数之前,包含电子邮件地址或URL的内容将被拒绝。我最初编写此内容是为了在字符串通过一些反垃圾邮件检查后对其进行格式化。如果您对其进行修改,以正确地允许+格式化电子邮件或URL以满足您的要求,请务必共享您的增强代码在此提供给可能感兴趣的任何人。谢谢。(y)我使用的是另一种解决方案。我在应用清理之前剥离URL和电子邮件,并在最后将其注入。不确定我会将其放在这个线程中的何处。从PHP7开始,不再支持/e修饰符
function format_punc($string){
    $punctuation = ';:';
    $spaced_punc = array(' ?', ' .', ' ,');
    $un_spaced_punc = array('?', '.', ',');
    $string = preg_replace("/([.,!?;:])+/iS","$1",$string);
    $string = preg_replace('/[[:space:]]+/', ' ', $string);
    $string = str_replace($spaced_punc, $un_spaced_punc, $string);
    $string = preg_replace('/(['.$punctuation.'])[\s]*/', '\1 ', $string);
    $string = preg_replace('/(?<!\d),|,(?!\d{3})/', ', ', $string);
    $string = preg_replace('/(\.)([[:alpha:]]{2,})/', '$1 $2', $string);
    $string = trim($string);
    $string = preg_replace('/([\.!\?]\s+|\A)(\w)/e', '"$1" . strtoupper("$2")', $string);
    if($string[strlen($string)-1]==','){
        $string = substr($string, 0, -1).'.';
    }
    return $string;
}