English 中文(简体)
PHP - Removing Duplicate Punctuation?
原标题:PHP - Removing Duplicate Punctuation?
  • 时间:2012-05-11 04:05:56
  •  标签:
  • php

附录 你们怎样! 页: 1

我希望尽可能消除背后图,如果出现类似于“!”的情况,则保留问题标记。 过滤体应如下:

<编码> Hello. How? 页: 1

解决该问题的最简明的解决办法是最佳的。 我期望,定期表达将是解决这一问题的最佳途径,但我对定期表达的了解是非常有限的,而且我不知道如何解决这一问题。 然而,用非常规解决办法,我比罚款还要高! 对你提供的无耻的简单易懂的法典的解释也是空洞的。

谢谢!

最佳回答
$str = preg_replace( ~[?!]{2,}~ ,  ? , preg_replace( ~([.,!?])(\1+)~ ,  \1 , $str));

preg_replace calls (thanks to Alix Axel)

$str = preg_replace(array( ~([.,!?])(\1+)~ ,  ~[?!]{2,}~ ), array( \1 ,  ? ), $str);

Just enumerate all the punctuation you care of in the braces

<>UPD: 处理!>?

Explanation of what it all means:

preg_replace( ~([.,!?])(\1+)~ ,  \1 , $str)

<代码>([,!])(1+)系指-发现任何<代码>,!>,条件是在此之前至少有一个相同的特性<代码>/1+,即<代码>/1-指的是以前的匹配,+ > 至少有一个。

仅用单一果园取代所有这些。

外部表述<代码>[!]{2,} 系指find all ? 如果在浏览中至少有2个,将其改为?

问题回答

您可以使用前言:

$a="Hello... how are you!!?? Im bored!!!!!!"; echo preg_replace("/([.!?])+/iS","$1",$a);

=> Hello. how are you? Im bored!
$string = "Hello... how are you!!?? I m bored!!"
$new_string = $string;
foreach(array( . , , , ? , ! ) as $value) {
  $i = ;
  do {
    $prev_string = $new_string;
    $string = str_replace($value . $value,$value,$string;
    $i++;
  } while ($string !== $prev_string && $i<100)
}

这消除了重复,但不是?

I think this second solution will work, keeping the FIRST of your "bad_chars". If you want to keep the LAST one, there are solutions to that as well.

<?php
$string = str_split($string);
$new_string = array();
$i = 0;
foreach($string as $key => $char) {
    echo  Processing:   . $char .  <br /> ;
    $prev_key = $key - 1;
    $prev_char = $string[$prev_key];
  if($i!== 0) {
    if(in_array($char,$bad_chars) && in_array($prev_char,$bad_chars) ) {
      // do nothing
    } else {
      $new_string[] = $char;
    }
  } else {
    $prev_char = $char;
    $new_string[] = $prev_char;
  }
  $i++;
}
$string = implode(  ,$string);
$new_string = implode(  ,$new_string);
?><br />

<?php echo $string; ?><br />
<?php echo $new_string; ?><br />




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签