PHP将字符串用双引号引起来 [英] PHP Wrap a string in double quotes

查看:265
本文介绍了PHP将字符串用双引号引起来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将用户输入的字符串用双引号引起来,以便输出,但是我想确保最后不要在字符串的两边都用双双引号引起来.例如,如果用户发布

I'm attempting to wrap a user-inputted string in double quotes for output but I want to make sure I don't end up with double double quotes on either side of the string. For example, if a user posts

你好"

我不想把它变成

"你好"

""Hello""

我可以使用下面的代码轻松地做到这一点,但是我担心如果我遍历许多字符串,这样做可能会变慢.

I can do this fairly easily using the code below, however I'm concerned that this may get slow if I'm looping through lots of strings.

$string = '"'.trim($string,'"').'"';

如果有人有更好的方法可以做到这一点,那就太好了.同样,如果有人可以确认我的方法还可以,我会很高兴.

If anyone has a better way of doing this, that'd be great. Equally, if anybody can confirm that my way is fine, I'll be happy.

谢谢

推荐答案

这正是我解决该问题的方法.如果您在应用程序运行缓慢方面遇到问题,仅需担心代码运行缓慢,就可以将其追溯到trim语句.

This is exactly how I would solve this problem. It's only worth worrying about the code being slow if you have a problem with the application being slow, and you can trace it down to the trim statements.

著名的编程语录是"过早的优化是万恶之源"-请参见维基百科文章对此有更多链接.

A well known programming quote is "Premature Optimisation is the root of all evil" - see the wikipedia article linked for more on this.

这篇关于PHP将字符串用双引号引起来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆