文本字段中的“&”符号与字段数据的显示混在一起
我有一个用 php 和 mysql 编写的客户管理系统。一家公司使用它来记录客户详细信息以进行营销和订单。我的表单上有多个字段,其中之一是公司名称。
这些字段被导入到电子邮件发送器中,以向客户发送报价等。字段内容从数据库中提取并存储在各种字符串中。例如,公司名称存储在$name
中。
我发现的问题是,如果企业输入诸如 Joe Blogs & Joe Blogs & 之类的企业名称, Sons
最终导入到电子邮件系统的是 Joe Blogs
,就是这样,& 号之后的任何内容都被切断,包括 & 号。如果他们使用 Joe Blogs and Sons
,则效果很好,当电子邮件发出时,它会显示 Dear, Joe Blogs and Sons
,但如果有人输入 &
code> 它最终会变成 Dear, Joe Blogs
。
我是否正确地说 str_replace
或 preg_replace
之类的东西是解决这个问题的方法还是逃避 &
的方法? 。
I have a customer mangement system that I have programmed in php and mysql. A company uses it to log their customer details for marketing and orders. There are various fields on my form, one of them being Business Name.
The fields are imported into an emailer to send to the customers quotes etc. The field contents are pulled in from the database and stored in various strings. Business name for example is stored in $name
.
The problem I have found is if the business enters a business name such as Joe Blogs & Sons
what ends being imported into the email system is Joe Blogs
and that is it, anything after the ampersand is cut off, including the ampersand. If they use Joe Blogs and Sons
this works fine and when the email goes out it says Dear, Joe Blogs and Sons
, but if someone enters &
it just ends up as Dear, Joe Blogs
.
Would I be correct in saying something like str_replace
or preg_replace
is the way to go with this or a way to escape the &
's ? .
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
htmlentities()
或htmlspecialchars()
对特殊 HTML 实体进行编码,以便注入到 HTML 上下文中。请注意,这些函数对于任何其他上下文都不够。示例:好:
坏:
Use either
htmlentities()
orhtmlspecialchars()
to encode special HTML entities for injection into an HTML context. Please note that these functions are not sufficient for any other context. Example:Good:
Bad:
尝试转换&进入
&
。有多种 php 函数可以实现这一点。选择一个适合您的函数
try to convert & into
&
.there are various php functions for that.choose one that suits you
这取决于您所说的“导入电子邮件程序”是什么意思。
我想你只需要将
&
转换为&
的 - 可能最好只使用 htmlentities 围绕每个变量(假设它们不包含需要解析的 HTML)作为“字符串清理”的一部分过程。It depends what you mean by "imported into an emailer".
I'd imagine you just need to convert the
&
's into&
's - probably best just to use htmlentities around each variable (assuming they don't hold HTML that needs parsed) as part of your "string cleaning" process.