strpos()有时只能工作

问题描述 投票:0回答:2

我一直在尝试在我的代码中使用PHP函数strpos()。我不知道为什么这段代码不起作用。如果我尝试使用包含我的'b'标签的自定义字符串,它会起作用。如果我尝试通过我的数组获取字符串,它不起作用。我也试过这种方式:strpos('<b>',$textArray[$i]),但它没用。

PS:(字符串)演员表不起作用。

<?php
$path="description/1.txt";
    $myfile = fopen($path, "r");
    $buffer="";
    if ($myfile) {
        $buffer=htmlspecialchars(fread($myfile,filesize($path)), ENT_QUOTES, 'UTF-8');
        fclose($myfile);
    }
    $textArray= explode(" ", $buffer);
    for($i=0;$i<count($textArray);$i++){

        $tmp="test string <b> :) ";

        if(strpos($textArray[$i],'<b>',0)===false){
            echo "Didn't find  ";
        }else{
            echo "YEES :D  ";
        }
    }

 ?>

数组var_dump():

array(10) {
 [0]=>
 string(15) "sadsad<b>"
 [1]=>
 string(16) "asdw</b>ad"
 [2]=>
 string(24) "a<b>sdsa</b>"
 [3]=>
 string(9) "<b>"
 [4]=>
 string(4) "dsad"
 [5]=>
 string(3) "sad"
 [6]=>
 string(3) "sad"
 [7]=>
 string(9) "asdsadsad"
 [8]=>
 string(6) "sadsad"
 [9]=>
 string(8) "sadsadas"
}
php explode strpos
2个回答
1
投票

当你使用htmlspecialchars()<之类的东西转换为&lt;时,你将永远找不到<b>

所以删除对htmlspecialchars()的调用。

$buffer=fread($myfile,filesize($path));

0
投票

尝试删除htmlspecialchars它应该工作。

© www.soinside.com 2019 - 2024. All rights reserved.