php去掉部分字符串

发布于 2024-10-11 08:19:13 字数 577 浏览 4 评论 0原文

我正在尝试构建一个简单的 sql 语句:

    // build sql statement          
    $sql = "select * from some_tbl where "; 
    if(strlen($mydetails['city']) > 0) { 
        $sql .= "cityname in (".$mydetails['city'].") and "; 
    } 
    $sql .= 'fromdate <= expirydate and expirydate >= curdate() order by rand()';

但是 $sql 缺少 << 之间的所有内容和>。调试器将 $sql 的值显示为:

    select * from tbl_adsinfo where fromdate = curdate() order by rand()

这太基本了,我只是迷路了。我不认为<或>特殊字符对吗?我尝试过转义它们并使用双引号代替,但结果是一样的。

这是怎么回事?

I'm trying to build a simple sql statement:

    // build sql statement          
    $sql = "select * from some_tbl where "; 
    if(strlen($mydetails['city']) > 0) { 
        $sql .= "cityname in (".$mydetails['city'].") and "; 
    } 
    $sql .= 'fromdate <= expirydate and expirydate >= curdate() order by rand()';

But $sql is missing everything between < and >. The debugger shows the value of $sql as:

    select * from tbl_adsinfo where fromdate = curdate() order by rand()

This is so basic I'm just lost. I don't think that < or > are special characters right? I've tried escaping them and using double quotes instead and it's the same.

What's up here?

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

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

发布评论

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

评论(4

琴流音 2024-10-18 08:19:13

您不会碰巧通过 striptags() 函数运行 $sql 变量,对吗?这与删除“<= expirydate and expirydate >”是一致的,因为它会假设它是一个 HTML 标签。

You wouldn't happen to be running the $sql variable through the striptags() function, would you? This would be consistent with it stripping out "<= expirydate and expirydate >", as it would assume it to be an HTML tag.

反差帅 2024-10-18 08:19:13

我不确定是什么导致了这个问题。显然,有一些东西剥离了 html 标签,也许是一种安全措施。一个建议是尝试替换 '<'和“>”及其 ASCII 代码:

 $sql .= 'fromdate '.chr(60).'= expirydate and expirydate '.chr(62).'= curdate() order by rand()';

编辑:
您还可以使用 NOT BETWEEN 语句,如下所示:

 $sql .= '(expirydate NOT BETWEEN fromdate AND curdate()) AND (expirydate NOT BETWEEN fromdate AND 17530101) ORDER BY rand()

17530101 意味着日期时间可能的最小值。在前面的代码中,您要检查到期日期是否不在 fromdate 和 curdate() 之间,并且到期日期是否不小于其中任何一个。这意味着 expirydate 应该大于 fromdate,curdate 以使语句返回 true,这就是您想要实现的目标。

I am not sure what is causing the problem. Apparently there is something stripping the html tags off as a sort of security maybe . One suggestion is to try to replace the '<' and '>' with their ASCII codes:

 $sql .= 'fromdate '.chr(60).'= expirydate and expirydate '.chr(62).'= curdate() order by rand()';

edit:
You may also use NOT BETWEEN statement like below:

 $sql .= '(expirydate NOT BETWEEN fromdate AND curdate()) AND (expirydate NOT BETWEEN fromdate AND 17530101) ORDER BY rand()

The 17530101 is meant to be the least value for a datetime possible. In the previous code you are checking if the expirydate isn't between fromdate and curdate() AND expirydate isn't less than any of them. That means that expirydate should be greater than fromdate,curdate for the statement to return true, which is what you are trying to achieve.

梦幻的味道 2024-10-18 08:19:13

为什么不使用 BETWEEN 运算符?

$sql .= 'expirydate BETWEEN fromdate AND curdate() ORDER BY rand()';

编辑:

仔细查看您还需要等于,尝试将操作拆分

$sql .= 'fromdate <= expirydate and expirydate >= curdate() order by rand()';

为:

$sql .= 'expirydate >= curdate() ';
$sql .= 'AND fromdate <= expirydate ';
$sql .= 'ORDER BY rand()';

或反转运算符的顺序:首先使用 =

$sql .= 'AND fromdate =< expirydate ';
$sql .= 'ORDER BY rand()';

Why not use the BETWEEN operator?

$sql .= 'expirydate BETWEEN fromdate AND curdate() ORDER BY rand()';

EDIT:

Looking it over you need the equals as well, try splitting up the actions

$sql .= 'fromdate <= expirydate and expirydate >= curdate() order by rand()';

to:

$sql .= 'expirydate >= curdate() ';
$sql .= 'AND fromdate <= expirydate ';
$sql .= 'ORDER BY rand()';

Or reverse the order of the operator: make the = first

$sql .= 'AND fromdate =< expirydate ';
$sql .= 'ORDER BY rand()';
你在我安 2024-10-18 08:19:13

好的,我明白了。该问题与 PHP 无关,而是 Zend Studio 和/或 XDebugger。

让 Zend 使用调试器花了我好几天的时间,而且我确信它可能并不完全符合我让它运行的方式。

如果您在 Zend Studio 中调试时将鼠标悬停在变量上,则会弹出一个小窗口来显示该变量的内容(有时)。该窗口确实会截断“<”后面的所有内容。该变量仍然包含正确的字符串,但 IDE/调试器非常具有误导性。不幸的是,由于这个特殊字符是一个常见问题,因为 html 解析这是一个非常令人困惑的错误。如果您尝试比较 php 页面的输出或者查看非常长的输出(对于非常长的字符串,您似乎只能在调试变量窗口中看到 1024 个字符,因此字符串可能会被截断),那么问题就会变得更加复杂。

遗憾的是,这仍然是迄今为止我为 PHP 开发的最好的 IDE。

Ok, I got it. The problem had nothing to do with PHP, it was Zend Studio and/or the XDebugger.

Getting Zend to use a debugger at all took me days and I'm sure it probably isn't entirely kosher the way I got it to run.

If you hover over a variable while debugging in Zend Studio a little window pops up to show you the contents of that variable (sometimes). This window does cut off anything that follows '<'. The variable still contains the correct string, but the IDE/Debugger is pretty misleading. Unfortunately since this particular character is a common problem because of html parsing this was a pretty confusing bug to identify. The issue is compounded if you try to compare output from a php page or if you are looking at very long output (for very long strings it seems you can only see 1024 characters in the debug variable window so the string may appear truncated).

Sadly, this is still the best IDE I've come up with for PHP so far.

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