dsaf32131 2015-07-21 17:11
浏览 27
已采纳

php验证POST中的空格

my question is this: I made a very simple adress book. The information is showing in html table and i just want to make as simple as possible function to delete an item from the book. I just want to copy the line with the information of the item and paste in the text input to delete it, butt I have problem with the too long white spaces in the string. String looks like '21-07-2015........ Coffe .......1.5...... Food', (the dots are showing how much space is between the words) but i just need one space between the words. Is there any way to do that without giving a deam how long is the white space between them. 'Trim is not helping here, just in case.'

if($_POST) {
$DELETE = $_POST['remove_line'];
$DELETE = str_replace('     ',' ',$DELETE);
$DELETE = str_replace(' ','!',$DELETE);
echo '<pre>'.print_r($DELETE,true).'</pre>';
$data = file("./info.txt");
$out = array();

foreach ($data as $line) {
    if (trim($line) != $DELETE) {
        $out[] = $line;
    }
}

$fop = fopen("./info.txt", "w+");
flock($fop, LOCK_EX);
foreach ($out as $line) {
    if($line) {
        if(fwrite($fop, $line)){
            $itemRemoved = 'Item has been removed correctly.';
        } else{
            $itemRemoved = 'You entered info item wrong.';
        };

    }
}
flock($fop, LOCK_UN);
fclose($fop);
}
  • 写回答

2条回答 默认 最新

  • doucan9079 2015-07-21 17:12
    关注

    You can replace two or more spaces with just one space by performing the following regular expression:

    $string = preg_replace('/\s+/', ' ', $string);

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。