3

I am having a problem. I have a string in a variable. I split the string in to an array using explode function. But strlen function shows different lengths when I echo the string as an array element and echo the same string in strlen function. 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
Riz
  • 185
  • 3
  • 7
  • 14
  • When I execute this very code, I get the expected result. What does the surrounding code look like? – Boldewyn Apr 29 '14 at 19:59
  • Are you getting yes at the end. I am getting no for this if statement. if(in_array('Early Evening 6pm-9pm Tuesday', $sc)){ echo 'yes'; } else { echo 'no'; } – Riz Apr 29 '14 at 20:03
  • It is something else: http://sandbox.onlinephpfunctions.com/code/b5f94476ca37fa91c642572116ffe114f7401860 – AbraCadaver Apr 29 '14 at 20:04
  • Actually I am getting string through a function which is $schedule = bp_get_the_profile_field_value();. So in my actual code which I am executing, instead of a string I have a function bp_get_the_profile_field_value() and this function gives me the string. so here is my actual code which is not working http://sandbox.onlinephpfunctions.com/code/58607a646367007a5f7433dbcb71ab8c50a53beb – Riz Apr 29 '14 at 20:11
  • Should I edit my question? – Riz Apr 29 '14 at 20:13
  • @Riz ah, the dangers of assumptions. :) – nl-x Apr 29 '14 at 20:40

1 Answers1

1

I think your string contains HTML tags that you just don't see in your browser.

Try adding

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

after your third line to make them visible, or view page source of your output.

nl-x
  • 11,762
  • 7
  • 33
  • 61
  • I think we are reaching our solution. Using above code I can see html elements along with my string. Array ( [0] =>

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

    ). Please tell me how to remove these html elements so that I can get simple "string Early Evening 6pm-9pm Tuesday, Early Evening 6pm-9pm Wednesday".
    – Riz Apr 30 '14 at 03:08
  • Thanks have done it. This helped me very much. I used strip_tags() to remove the html chars. – Riz Apr 30 '14 at 03:33
  • To remove simply do `$sc = array_map('strip_tags', $sc);` on line 4 (and first remove the line with `htmlspecialchars` you added for me. – nl-x Apr 30 '14 at 08:33