我正在用PHP开发一个网站.在其中,我将图像保存在服务器上的文件夹中.
我接受来自用户的名称,并希望将该名称用作图像名称.有时用户输入两个单词的名称.
所以我想删除两个单词之间的空格.例如,如果用户输入'Paneer Pakoda dish'.我想把它转换成'PaneerPakodaDish'.
我怎样才能做到这一点???我用了
1) str_replace(' ', '', $str);
2) preg_replace(' ', '', $str);
3) trim($str, ' ');
但这些并不是我所要求的.
我正在创建一个图像上传表单,我需要在将图像添加到我的数据库时将图像命名为用户名和狗的名称,并添加到我的上传文件夹中.
我目前的工作方式如下:
$user_name = $_POST['user_name'];
$dog_name  = $_POST['dog_name'];
$file_name = $imgDir . $user_name .'-'. $dog_name .'-'. $random_number . $ext;
插入表格时,我使用以下内容:
'file_name' => str_replace("../path/to/images/", "", $file_name),
文件名的当前输出如下: John-Fido-123456.jpg
如果用户输入这样的姓氏,我将如何删除文件名中的空格?
John Doe-Fido Doe-123456.jpg
更新:感谢@HiDeo我使用了他发布的dup链接,并且能够像这样想出来.
使用Post方法获取我需要的名称.我像这样划分了白色空间,它处理数据库中的文件名和文件夹中的实际图像.
$user_name  =   $_POST['user_name'];
$dog_name   =   $_POST['dog_name'];
$user_name = preg_replace('/\s+/', '', $user_name);
$dog_name  = preg_replace('/\s+/', '', $dog_name );
我正在进行一个php调用,并且响应在foreach函数中用于填充表.
我的表格单元格包含大量的空白,看起来它来自php响应.
下面是一个摘录,当您查看TD元素时,您可以看到额外的空间.
不确定是什么造成这种情况,或者如何将其移除,可能是修剪?
<div class="container-fluid" style="margin-top:2em;">
            <div class="row">
                <div class="col-lg-12" style="background: none;">
                    <h4>Test Drives</h4>
                                        <table class="table table-fixed table-sm table-hover" style="height: 100px; background:pink;">
                        <thead>
                            <tr>
                            <th>
                                Value
                            </th>
                            <th>
                                Date Submitted
                            </th>
                            <th>
                                 User submitted
                            </th>
                                <th>
                                 Market
                            </th>
                                <th>
                                 Notes
                            </th>
                                <th>
                                 Last Update by
                            </th>
                            </tr>
                        </thead>
                        <tbody>
                                                    <tr>
                                <td>
                                    2                                </td>
                                <td>
                                    2017-03-08                                </td>
                                <td>
                                    ADMIN                                </td>
                                <td>
                                    DE                                </td>
                                <td>
                                    This is a test                                </td>
                                <td>
                                    ADMIN                                </td>
                            </tr>
                                                    </tbody>
                    </table>
                </div>
            </div>
        </div>
下面是我的html中的PHP代码:
<div …我有一个人们输入多个代码的表单.现在,我想删除这些代码之间的空格.但是,我的代码似乎不起作用.有任何想法吗?
$codes = $_GET['codes'];
$spaces = strpos($codes, " ");
for($spaces; $spaces=0; $spaces--){
str_replace(" ", "", $codes);
echo $codes;
}
编辑:我只是尝试了别的东西,但它仍然无法正常工作.我的意思是回声每次给我原始的字符串.
$codes = $_GET['codes'];
$cleancodes = str_replace(" ", "", $codes);
$cleancodes = trim(preg_replace('/\s\s+/', ' ', $cleancodes));
echo "<br / >" . $cleancodes;