0

I'm trying to set the max length of each post on a site, but strlen() doesn't work with arrays. So I need to break it down to check each post in the array.

How could I adapt what I have to get this if statment to work correctly. The issue is the strlen() not accepting object.

    for($i = 0, $size = count($somePosts); $i < $size; ++$i) {
        if (strlen(utf8_decode($somePosts[$i])) > $max_length) {
            $offset = ($max_length - 3) - strlen($somePosts);
            $somePosts = substr($somePosts, 0, strrpos($reviewPosts, ' ', $offset)) . '...';
        }
    }

I'm using Doctrine to generate the array, which works fine.

Thanks.

Edit:

Error - Warning: strlen() expects parameter 1 to be string, object given

Edit 2:

No error messages now, but the code doesn't work in terms of limiting the length of the posts.

Dunney
  • 91
  • 2
  • 11

2 Answers2

3

You need to access the current array item like $somePosts[$i] and not $somePosts

for($i = 0, $size = count($somePosts); $i < $size; ++$i) {
    if (strlen(utf8_decode($somePosts[$i])) > $max_length) {
        $offset = ($max_length - 3) - strlen($somePosts[$i]);
        $somePosts[$i] = substr($somePosts[$i], 0, strrpos($reviewPosts, ' ', $offset)) . '...';
    }
}
IMSoP
  • 89,526
  • 13
  • 117
  • 169
Class
  • 3,149
  • 3
  • 22
  • 31
  • sorry that was a typo, yea the error is still expects string but object given. – Dunney Jan 21 '15 at 22:33
  • Then you need to convert the object to a array, which I'm not familiar with Doctrine but I imagine theres some function that does this. – Class Jan 21 '15 at 22:37
  • It's strange behaviour, because I tried changing the $somePosts to array from object, and it says array given. Then the code above says Object given. – Dunney Jan 21 '15 at 22:45
  • Maybe add some of the extra code that converts the array to your question – Class Jan 21 '15 at 22:50
  • The array is simply a list of String Entities from the DB declared in my ORM. The array works fine, the only issue is when I try to use strlen for each entity, so I don't end up with a page with huge blocks of text. Somewhere in there something isn't correct. Thanks for helping though. – Dunney Jan 21 '15 at 22:56
  • did you replace all `$somePosts` with `$somePosts[$i]`? – Class Jan 21 '15 at 23:00
  • yea, the error is "Warning: utf8_decode() expects parameter 1 to be string, object given" even if I use "$someArray = (array) $somePosts;" before the loop and use $someArray instead. – Dunney Jan 22 '15 at 00:59
0

As alternative you can use array_map

<?php

$strs = array('abcdefgh', 'defghjklmn', 'ghjefggezgzgezg');
$max = 5;
$strs = array_map(function($val) use ($max) {
    if (strlen($val.'') < $max) return $val.'';
    return substr($val.'', 0,$max-3).'...';
},$strs);

var_dump($strs);

EDIT implicit casts added to cast object to string

DarkBee
  • 16,592
  • 6
  • 46
  • 58
  • replace `$val` with `$val.''` to cast the object to string (in the if and the substr) – DarkBee Jan 21 '15 at 22:41
  • Catchable Fatal Error: Object of class "\bundle\Entity\..." could not be converted to string – Dunney Jan 21 '15 at 22:48
  • You should override/implement a `toString` method in your entity. See this [blog](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454) – DarkBee Jan 21 '15 at 22:50
  • Wrong blog? this isn't a html/regex problem – Class Jan 21 '15 at 22:52
  • true, nothing to do with that. The entity is declared as a String in ORM. – Dunney Jan 21 '15 at 22:54
  • Great link, got rid of the errors. Unfortunately my code for the length of the posts did nothing. – Dunney Jan 22 '15 at 01:07