PHP Question: How to echo an Array
-
Hi this is probably a very basic question, but here it goes. I’m displaying a list of related posts in my single.php and I’m showing the title and the excerpt and a custom field image. I would like to cut the length of the excerpt to one sentence for each related item.
By using preg_split as suggested here:
https://php.about.com/od/advancedphp/ss/php_preg_5.htmI have managed to return an array that contains each sentence. I have used this code:
<?php $str = $post->post_excerpt; $sentances= preg_split('/\./', $str, -1 , PREG_SPLIT_NO_EMPTY); print_r($sentances); ?>
This particular code, returns something like this:
Array ( [0] => Maecenas fermentum semper purus [1] => Fusce vitae tortor [2] => In laoreet ante ornare metus lobortis pretium [3] => Suspendisse eu libero [4] => Suspendisse facilisis [5] => Phasellus convallis, purus in imperdiet condimentum, nisi orci gravida ligula, eu pellentesque odio risus quis risus [6] => Aenean vitae pede at enim imperdiet varius [7] => Vivamus sodales feugiat est [8] => Nulla lobortis consectetuer magna )
I would like to only echo the very first sentence, how would i go about doing that?
- The topic ‘PHP Question: How to echo an Array’ is closed to new replies.