简体   繁体   English

简单的strpos()无法与“ [”字符一起使用,为什么?

[英]simple strpos() not working with “[” char, why?

why does if i do: 为什么要这样做:

if(strpos("[","[rgeger]")){ 
echo 'hey'; 
}

it doesn't prints anything? 它什么都不打印?

try this with a string: 尝试使用字符串:

function return_tags($keywords){
 if($keywords){
  $k_array = array();

  foreach($this->check($keywords) as $key=>$value){

    array_push($k_array, $value);
  }

  $last_array = array();
  foreach($k_array as $key=>$value){
    if(strpos("[", $value) && strpos("]", $value) && strlen($value) >= 2){
      $value = '<span class="tag">'.$value.'</span>';

    }
    array_push($last_array, trim($value));

  }
  return $last_array;

 }else{
  return false;
 }
}

string example 字符串示例

  $keywords = "freignferi eiejrngirj erjgnrit [llll] [print me as tag]";

did you see any <span> element printed in html? 您是否看到以html打印的任何<span>元素?

It looks like you swapped the arguments: 好像您交换了参数:

int strpos ( string $haystack , mixed $needle [, int $offset = 0 ] )

So if you want to know if a [ is in your string [rgeger] : 因此,如果您想知道[是否在字符串[rgeger]

if (strpos("[rgeger]", "[") !== false) { 
  echo 'hey'; 
}

(Source: http://php.net/strpos ) (来源: http : //php.net/strpos

The position returned is zero-indexed, so the first character is at position 0. When evaluating 0 as Boolean, it's false - so it doesn't get into the block. 返回的位置是零索引的,因此第一个字符位于位置0。将0评估为布尔值时,它是假的-因此它不会进入该块。

Fix with this: 解决此问题:

if (strpos('[rgeger]', '[') !== false)

Also the arguments are the wrong way round. 同样 ,论点也是错误的。 Don't upvote this post any more; 不要再赞这个帖子了; go upvote robbi's instead for spotting that one :) Eagle eyes robbi, EAGLE eyes :P 去投票罗比,而不是发现那一个:)鹰眼罗比,鹰眼:P

Because you have to check the return value of strpos() as it could be zero because [ it's at index zero, and zero evaluates to FALSE that's why it wouldn't enter your if block, so check that the return value it's FALSE and type boolean, not just integer zero, like this: 因为您必须检查strpos()的返回值,因为它可能为零,因为[它的索引为零,而零的计算结果为FALSE,这就是为什么它不会输入if块的原因,因此请检查返回值是否为FALSE并键入布尔值,不只是整数零,像这样:

if(strpos("[rgeger]","[") !== false){ 
  echo 'hey'; 
}

UPDATE: 更新:
The parameters were in wrong order too, the subject string comes first then the search string, I updated my code above to reflect that. 参数的顺序也错误,主题字符串首先出现,然后是搜索字符串,我在上面更新了代码以反映这一点。

Because its wrong. 因为它错了。 Strpos give false if the condition is false. 如果条件为假,则Strpos给出假。

if(strpos("[rgeger]","[") !== false){ 
    echo 'hey'; 
}

Edit: I have corrected my answer. 编辑:我已经纠正了我的答案。 Your parameter are in the wrong order. 您的参数顺序错误。 Its: 它的:

int strpos ( string $haystack , mixed $needle [, int $offset = 0 ] )

Because actually strpos can return 0 see doc . 因为实际上strpos可以返回0,请参阅doc

Warning This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. 警告此函数可能返回布尔FALSE,但也可能返回非布尔值,其值为FALSE。 Please read the section on Booleans for more information. 请阅读布尔值部分以获取更多信息。 Use the === operator for testing the return value of this function. 使用===运算符测试此函数的返回值。

So you have to compare with the false value directly. 因此,您必须直接与false值进行比较。

if(strpos("[","[rgeger]") !== false){ 
  echo 'hey'; 
}

EDIT :: 编辑::

Careful.. Look at the arguments order 小心..看参数顺序

int strpos ( string $haystack , mixed $needle [, int $offset = 0 ] )

Haystack is the string input. Haystack是字符串输入。 Needle is what you are seeking for. 针是您想要的。

if(strpos("[regeger]","[") !== false){ 
  echo 'hey'; 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM