1

I have issue with the PHP Array. I want to get the last element from array which is not null or not blank.

        $string  = '9580 County Road
                    Clarence Center, New York 14032 
                    TEL: 716-863-4133
                    FAX: 716-741-4810 
                    <a href="mailto:[email protected]">[email protected]</a>
                    <a href="http://www.wmssales.org" target="_blank">wmssales.org</a>
                    Randy Schaefer';
        $array=explode("\n",$string);
        $new_array = array_filter($array);
        $rev_new_array=array_reverse($new_array);
        for( $i = 0; $i < sizeof($rev_new_array); $i++)
        {
            if($rev_new_array[$i]!="")
            {
                echo $writter = $rev_new_array[$i];break;
            }
        }

when i search on google, I find some below link, but which is not useful for me :-

1
  • What you have is rather inelegant, but should work. What's the problem with it? Commented Nov 26, 2014 at 12:35

1 Answer 1

2

Try this.

Your string contains space characters at the end.

So, you need to trim it.

<?php
$string  = '9580 County Road
                        Clarence Center, New York 14032 
                        TEL: 716-863-4133
                        FAX: 716-741-4810 
                        <a href="mailto:[email protected]">[email protected]</a>
                        <a href="http://www.wmssales.org" target="_blank">wmssales.org</a>
                        Randy Schaefer
                          ';
$string = trim($string);
$array=explode("\n",$string);
echo '<pre>';
print_r(end($new_array))
echo '</pre>';
?>

Working Demo

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

2 Comments

Thank you much for save my time I already invest 1 hour on this problem. I also use trim(), but in my case i use into the for loop
accepting time is 10 min and you are superfast for answering

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.