str_replace 需要堆
我遇到了 str_replace 函数的问题,请参阅下面的代码:
$query = "SELECT title FROM zakov WHERE chnt='$atd_nad'";
$str = str_replace("Example.com_", "","$query");
$result = mysql_query($str) or die('Errant query: '.$str);
我想要的是将单词“Example.com_”替换为空“”,但它对我不起作用!我不知道为什么。 在“标题”行中,您可以找到类似“Example.com_nameofsmthng”的内容 所以我想要的是只保留“nameofsmthng”这个词,并且将每个单词的开头保留为大写字母,最终得到像“NameOfSmthng”这样的东西
I face a problem with the str_replace function, see the code below :
$query = "SELECT title FROM zakov WHERE chnt='$atd_nad'";
$str = str_replace("Example.com_", "","$query");
$result = mysql_query($str) or die('Errant query: '.$str);
What I want is to replace the word " Example.com_ " with nothing "" but it did not work for me ! I do not know why.
In the row 'title' you can find something like this " Example.com_nameofsmthng "
So what I want is to keep just the word "nameofsmthng" and also to keep the begining of each word of it in capital letter to have finally somethin like "NameOfSmthng"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这很好用。 快速尝试。我的假设是您输入错误 $atd_nad 或者该值不正确。
This works fine. Try it quickly. My assumption is that you mistyped $atd_nad or the value is incorrect.
编辑:嗯,我想我误解了您尝试替换查询字符串中的字符串而不是数据库中的示例?
你可以让 mysql 为你做替换,这应该比让 php 做更快。
或者,您可以使用更新替换数据库中的所有匹配项,然后只需选择标题。
希望这有帮助。
Edit: hmm i think I misunderstood the example your trying to replace the string in the query string instead of the database?
You could make mysql do the replacement for you which should be faster then making php do it.
Or you could replace all occurrences in the database with an update and then just select the title.
Hope this helps.
我相信这就是您正在寻找的。您的代码试图在查询中替换它,这是没有意义的。您需要在实际记录中替换它。这会将“某物”替换为“”。或者,如果您已经将它们存储在数组或其他内容中,您只需循环数组并进行替换即可。基本上:对记录进行操作,而不是对查询进行操作。
Is what I believe you're looking for. Your code is trying to replace it in the query, which doesn't make sense. You need to replace it in the actual records. This will replace "something" with "". Alternatively, if you've already stored them in an an array or something you would just loop over the array and do the replacement. Basically: operate on the records, not on the query.