【问题标题】:Add/fix punctuation in php在 php 中添加/修复标点符号
【发布时间】:2012-08-25 19:58:15
【问题描述】:

我正在慢慢完善 PHP 中用于清理用户输入的标点符号修复功能。该函数目前在标点符号后添加空格,在标点符号前删除空格,并将每个句子的第一个单词大写。我见过一些人在寻找类似的功能,所以我很高兴分享我到目前为止所拥有的东西。它非常接近我想要的位置,但是,当它在逗号后添加空格时,当逗号在 1,000 等数字内时,应避免这样做?也许有办法缩短我所拥有的但仍然达到相同的结果?感谢您的宝贵时间...

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;
}

【问题讨论】:

  • 如何确定逗号是千位分隔符,或者您正在处理数字的枚举?
  • 这就是我发布此内容的原因...我希望有人可以帮助推进该功能以判断其是否处理数字。
  • 有些国家使用不同的轮廓线来表示 1000 秒,也许这可以从设置中推断出来......(只是说)

标签: php punctuation


【解决方案1】:

这是我更新的 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;
}

如果您花时间压缩此代码并创建仍然返回相同结果的东西,请分享!谢谢你,享受!

【讨论】:

  • 如何保护电子邮件地址或网址中的标点符号?
  • 对于我的使用,在将字符串传递给此函数之前,会拒绝包含电子邮件地址或 url 的内容。我最初写这个是为了在它通过一些反垃圾邮件检查后格式化字符串。如果您修改它以正确允许+格式化电子邮件或网址以满足您的要求,请在此处为可能感兴趣的任何人分享您的增强代码。谢谢。 (y)
  • 我正在使用不同的解决方案。我在应用清理之前剥离了 url 和电子邮件,并在最后将它们重新注入。不知道我会把它放在这个线程中的什么地方。
  • 自 PHP 7 起不再支持 /e 修饰符
【解决方案2】:

我认为正则表达式应该是 ([^0-9][.][^0-9])[\s]*

preg_replace('/([^0-9]['.$punctuation.'][^0-9])[\s]*/', '\1 ', $string)

Link to regexp test

【讨论】:

  • 谢谢,很接近...但它会导致空格,例如... "String,word. 2,000"
【解决方案3】:

这有点复杂,但它应该能让你朝着正确的方向前进:

<?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

    }
}

【讨论】:

    猜你喜欢
    • 2019-07-05
    • 1970-01-01
    • 2016-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-07
    相关资源
    最近更新 更多