简体   繁体   English

php strlen显示了不同长度的数组元素

[英]php strlen shows different length of array elements

I am having a problem. 我遇到了问题。 I have a string in a variable. 我在变量中有一个字符串。 I split the string in to an array using explode function. 我使用explode函数将字符串拆分为数组。 But strlen function shows different lengths when I echo the string as an array element and echo the same string in strlen function. 但是当我将字符串作为数组元素回显并在strlen函数中回显相同的字符串时,strlen函数显示不同的长度。 Below is my code 以下是我的代码

 echo $schedule = 'Early Evening 6pm-9pm Tuesday, Early Evening 6pm-9pm Wednesday';
    $scr = explode(",",$schedule);
    $sc = array_map('trim', $scr);
    print_r($sc);
    echo strlen($sc[0]);
    echo $sc[0];
    echo '<br />';
    echo 'string "Early Evening 6pm-9pm Tuesday" has '.strlen('Early Evening 6pm-9pm Tuesday'). ' chars<br />';
if(in_array('Early Evening 6pm-9pm Tuesday', $sc)){ echo 'yes'; } else { echo 'no'; }

and here is output: 这是输出:

        Early Evening 6pm-9pm Tuesday, Early Evening 6pm-9pm Wednesday

        Array ( [0] =>
        Early Evening 6pm-9pm Tuesday [1] => Early Evening 6pm-9pm Wednesday

        ) 
    151 //length of first element of array
        Early Evening 6pm-9pm Tuesday //first element of an array
        string "Early Evening 6pm-9pm Tuesday" has 29 chars
no //I need yes here

I think your string contains HTML tags that you just don't see in your browser. 我认为您的字符串包含您在浏览器中看不到的HTML标记。

Try adding 尝试添加

$sc = array_map('htmlspecialchars', $scr);

after your third line to make them visible, or view page source of your output. 在您的第三行之后使其可见,或查看输出的页面源。

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

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