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
6
  • When I execute this very code, I get the expected result. What does the surrounding code look like? Commented Apr 29, 2014 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'; } Commented Apr 29, 2014 at 20:03
  • It is something else: sandbox.onlinephpfunctions.com/code/… Commented Apr 29, 2014 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 sandbox.onlinephpfunctions.com/code/… Commented Apr 29, 2014 at 20:11
  • Should I edit my question? Commented Apr 29, 2014 at 20:13

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I think we are reaching our solution. Using above code I can see html elements along with my string. Array ( [0] => <p><a href="dev.optimizebusinessgrowth.com/gopaperboy/members/…" rel="nofollow">Early Evening 6pm-9pm Tuesday</a> [1] => <a href="dev.optimizebusinessgrowth.com/gopaperboy/members/…" rel="nofollow">Early Evening 6pm-9pm Wednesday</a></p> ). 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".
Thanks have done it. This helped me very much. I used strip_tags() to remove the html chars.
To remove simply do $sc = array_map('strip_tags', $sc); on line 4 (and first remove the line with htmlspecialchars you added for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.