【问题标题】:Matching a string to another string with PHP使用 PHP 将一个字符串与另一个字符串匹配
【发布时间】:2017-04-10 07:33:40
【问题描述】:

我试图在 While 循环中过滤一些条目。我正在尝试使用strpos 函数,不幸的是,当我使用变量作为针时,我没有得到结果。

没有结果: $tra = strpos($HRreq, $entry_type)

结果:$tra = strpos($HRreq, "string");

但是,我需要它在 while 循环中是可变的。

$select_exec = odbc_exec($amos_db,$select_personnel);
    while ($row = odbc_fetch_array($select_exec)){
        $username = $row['user_sign'];
        $entry_type = $row['entry_type'];
        $fullname1 = $row['lastname'] . $row['firstname'];
        $fullname = trim(preg_replace('/ +/', ' ', preg_replace('/[^A-Za-z0-9 ]/', ' ', urldecode(html_entity_decode(strip_tags($fullname1))))));
        $userno = $row['employee_no_i'];
        $tra = strpos($HRreq, "$entry_type");
        echo $entry_type ." = ". $tra;
        }

我添加了部分代码,因为代码长度超过 500 行。我希望这足以让我了解我想要完成的工作

【问题讨论】:

  • 能把你用过的代码加进去吗?
  • 你确定变量不为空或空白吗?
  • 我添加了更多代码,我尝试了echo$entry_type,然后是$tra,它只返回$entry_type,但$tra为空
  • $HRreq 变量的值是多少?它似乎没有定义。
  • 我已经在文件开头设置了,这个变量是相当长的字符串。这么久了,我还没有将它添加到代码中。

标签: php string search strpos


【解决方案1】:

我发现数据库返回变量后带有空格。我添加了trim(),问题就解决了。这是一个愚蠢的错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-16
    • 1970-01-01
    • 2021-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-26
    相关资源
    最近更新 更多