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条)

报告相同问题?

悬赏问题

  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式
  • ¥50 关于多次提交POST数据后,无法获取到POST数据参数的问题
  • ¥15 win10,这种情况怎么办
  • ¥15 如何在配置使用Prettier的VSCode中通过Better Align插件来对齐等式?(相关搜索:格式化)
  • ¥100 在连接内网VPN时,如何同时保持互联网连接
  • ¥15 MATLAB中使用parfor,矩阵Removal的有效索引在parfor循环中受限制
  • ¥20 Win 10 LTSC 1809版本如何无损提升到20H1版本
  • ¥50 win10 LTSC 虚拟键盘不弹出
  • ¥30 微信小程序请求失败,网页能正常带锁访问
  • ¥15 Matlab求解微分方程,如何用fish2d进行预优?