str_replace 需要堆

发布于 2024-12-20 02:51:21 字数 409 浏览 0 评论 0原文

我遇到了 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 技术交流群。

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

发布评论

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

评论(3

本宫微胖 2024-12-27 02:51:21
$atd_nad = 'Foobar Example.com_nameofsmthng Bazbat';
$query   = 'SELECT title FROM zakov WHERE chnt="' . $atd_nad . '"';
$str     = str_replace('Example.com_', '', $query);
echo $str; // SELECT title FROM zakov WHERE chnt="Foobar nameofsmthng Bazbat"

这很好用。 快速尝试。我的假设是您输入错误 $atd_nad 或者该值不正确。

$atd_nad = 'Foobar Example.com_nameofsmthng Bazbat';
$query   = 'SELECT title FROM zakov WHERE chnt="' . $atd_nad . '"';
$str     = str_replace('Example.com_', '', $query);
echo $str; // SELECT title FROM zakov WHERE chnt="Foobar nameofsmthng Bazbat"

This works fine. Try it quickly. My assumption is that you mistyped $atd_nad or the value is incorrect.

一身骄傲 2024-12-27 02:51:21

编辑:嗯,我想我误解了您尝试替换查询字符串中的字符串而不是数据库中的示例?


你可以让 mysql 为你做替换,这应该比让 php 做更快。

$query = "SELECT REPLACE(title, 'Example.com_', '') as newtitle FROM zakov WHERE chnt='$atd_nad'";
$resultset = mysql_query($query) or die('Errant query:  '.$query);  
$result = mysql_fetch_assoc($query);
echo $result['newtitle'];

或者,您可以使用更新替换数据库中的所有匹配项,然后只需选择标题。

UPDATE zakov SET title = REPLACE(title, 'Example.com_', '');

希望这有帮助。

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.

$query = "SELECT REPLACE(title, 'Example.com_', '') as newtitle FROM zakov WHERE chnt='$atd_nad'";
$resultset = mysql_query($query) or die('Errant query:  '.$query);  
$result = mysql_fetch_assoc($query);
echo $result['newtitle'];

Or you could replace all occurrences in the database with an update and then just select the title.

UPDATE zakov SET title = REPLACE(title, 'Example.com_', '');

Hope this helps.

如日中天 2024-12-27 02:51:21
while($row = mysql_fetch_assoc($result)) {
    $title = str_replace("something", "", $row['title']);
}

我相信这就是您正在寻找的。您的代码试图在查询中替换它,这是没有意义的。您需要在实际记录中替换它。这会将“某物”替换为“”。或者,如果您已经将它们存储在数组或其他内容中,您只需循环数组并进行替换即可。基本上:对记录进行操作,而不是对查询进行操作。

while($row = mysql_fetch_assoc($result)) {
    $title = str_replace("something", "", $row['title']);
}

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.

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