检测文本区域中复制的选项卡以进行数据库输入

发布于 2024-11-16 16:45:47 字数 541 浏览 3 评论 0原文

我有一个文本区域,可以让用户编写自己的输入,并将其保存到数据库中。

大多数用户从 Word 或类似程序复制文本,这些程序也会将选项卡复制到文本区域中。

当选项卡保存到数据库时,它会自动将它们转换为一个空格。

我想将选项卡保存为数据库中的正确选项卡或一组空白,但是我无法首先捕获这些选项卡。

这是我到目前为止所尝试过的(在从用户获取输入之后并在保存到数据库之前)

str_replace("\t", "    ", $string);
str_replace(chr(9), "     ", $string);

但是,我可以说选项卡甚至没有被捕获,因为我尝试这样做:

if (strpos($string, "\t") !== false)
{
    exit("The tab was found");
}  

“退出”永远不会被调用。

我已经广泛搜索了这个问题的答案,但是我还没有找到任何修复方法。

我真的很感激对此的一些帮助。

I have a textarea that lets users write their own input which gets saved into the database.

The majority of the users copy their text from word or similar program, which also copies their tabs into the textarea.

When the tabs get saved to the database it is automatically converting them to just one space.

I want to save the tabs as either proper tabs into the database or a set number of whitespaces, however I can't catch the tabs in the first place.

This is what I have tried so far (after getting the input from the user and before saving to the database)

str_replace("\t", "    ", $string);
str_replace(chr(9), "     ", $string);

However, I can tell that the tabs aren't even being caught because I try this:

if (strpos($string, "\t") !== false)
{
    exit("The tab was found");
}  

The 'exit' never gets called.

I have searched extensively for an answer to this, however I haven't been able to find any fixes.

I would really appreciate some help on this one.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

假扮的天使 2024-11-23 16:45:47

Nathan Bell 的评论“在看到值之前是否对表单值进行任何预处理?”。这让我思考。

我使用 codeigniter 作为我的 php 框架。经过一些研究并查看源代码后,我发现内置的安全类将我的所有选项卡格式化为一个空格,就像这样......

if (strpos($str, "\t") !== FALSE)
{
    $str = str_replace("\t", ' ', $str);
}

所以我所做的就是将源代码更改为:

if (strpos($str, "\t") !== FALSE)
{
    $str = str_replace("\t", '      ', $str);
}

现在它正在工作。所以感谢内森和其他所有提供帮助的人!

Nathan Bell's comment "Is there any pre-processing being done on the form values before you see the value?". That got me thinking.

I am using codeigniter as my php framework. After doing some research and looking at the source code I found that the built in security class was formatting all my tabs into a single space, like so...

if (strpos($str, "\t") !== FALSE)
{
    $str = str_replace("\t", ' ', $str);
}

So all I did was change the source code to this:

if (strpos($str, "\t") !== FALSE)
{
    $str = str_replace("\t", '      ', $str);
}

And it's working now. So thank you Nathan and everyone else who assisted for the help!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文