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.

1
  • You can use array walk to alter every item within an array Commented Jan 21, 2015 at 22:33

2 Answers 2

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)) . '...';
    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

sorry that was a typo, yea the error is still expects string but object given.
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.
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.
Maybe add some of the extra code that converts the array to your question
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.
|
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

6 Comments

replace $val with $val.'' to cast the object to string (in the if and the substr)
Catchable Fatal Error: Object of class "\bundle\Entity\..." could not be converted to string
You should override/implement a toString method in your entity. See this blog
Wrong blog? this isn't a html/regex problem
true, nothing to do with that. The entity is declared as a String in ORM.
|

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.