jimii Posted June 16, 2022 Share Posted June 16, 2022 An issue I've lived with too long (not a coder - self-taught to a point but I cant properly solve this one) My word press has a plugin that delivers a text output from a self-publish form-field that I need to restrict. "Tagline" in code below. between --> <-- <?php if($property['custom_attribute_overview'] || $property['tagline']): ?> <li class="property_tagline"> <?php if($property['custom_attribute_overview']): ?> <?php echo $property['custom_attribute_overview']; ?> <?php elseif($property['tagline']): ?>--> <?php echo $property['tagline']; ?> <-- <?php endif; ?> </li> <?php endif; ?> I'd be happy if it either simply truncated after x characters or ended ... after x characters or some specified length Any guidance would be appreciated. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/314931-trim-or-limit-characters-in-echo-output/ Share on other sites More sharing options...
Barand Posted June 16, 2022 Share Posted June 16, 2022 Use substr() echo substr($property['tagline'], 0, $x) . '...'; Why all the <?php ?> tags? Quote Link to comment https://forums.phpfreaks.com/topic/314931-trim-or-limit-characters-in-echo-output/#findComment-1597353 Share on other sites More sharing options...
mac_gyver Posted June 16, 2022 Share Posted June 16, 2022 1 hour ago, Barand said: Why all the <?php ?> tags? Wordpress template code, i.e. make it look like you are doing a lot of work that's worthy of getting paid for. 1 Quote Link to comment https://forums.phpfreaks.com/topic/314931-trim-or-limit-characters-in-echo-output/#findComment-1597355 Share on other sites More sharing options...
jimii Posted June 16, 2022 Author Share Posted June 16, 2022 @ Barand, Thank you, I tried it on two sites. One resulted in a fatal error message (was able to undo) . The other site had no impact on another.... (I cant explain why all the php tags to be honest - its an older plugin originally from 2013 / 14 thereabouts. I got myself up to speed enough to customize some of the lines and code in my own site, but never picked up enough to see the efficiency or inefficiency of the original coding or excess php tags.. The plugin was or is wp-property - this is from the propertyoverview.php setup for listing properties in a list or gallery format. I do appreciate the the info and guidance offered. (The image displays the issue I am trying to resolve). (Perhaps I am better to try and truncate from the source text field) Quote Link to comment https://forums.phpfreaks.com/topic/314931-trim-or-limit-characters-in-echo-output/#findComment-1597385 Share on other sites More sharing options...
maxxd Posted June 16, 2022 Share Posted June 16, 2022 Is the issue that it's showing too much content or that it's overflowing on the right edge? Quote Link to comment https://forums.phpfreaks.com/topic/314931-trim-or-limit-characters-in-echo-output/#findComment-1597386 Share on other sites More sharing options...
jimii Posted June 17, 2022 Author Share Posted June 17, 2022 @ Barand, Thank you, I tried it on two sites. One resulted in a fatal error message (was able to undo) . The other site had no impact on another.... (I cant explain why all the php tags to be honest - its an older plugin originally from 2013 / 14 thereabouts. I got myself up to speed enough to customize some of the lines and code in my own site, but never picked up enough to see the efficiency or inefficiency of the original coding or excess php tags.. The plugin was or is wp-property - this is from the propertyoverview.php setup for listing properties in a list or gallery format. I do appreciate the the info and guidance offered. (Perhaps I am better to try and truncate from the source text field) Quote Link to comment https://forums.phpfreaks.com/topic/314931-trim-or-limit-characters-in-echo-output/#findComment-1597388 Share on other sites More sharing options...
jimii Posted June 17, 2022 Author Share Posted June 17, 2022 @maxxd Thank you for the question of clarity. Re too much content or overflowing: It is that it is showing too much content (there is no limit on the text input field that ). So self-publishers input a paragraph into what should only be a 5 or 6 word headline... (I believe I can css the overflow) Open to options (restrict input source field or the public facing side). Any guidance is appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/314931-trim-or-limit-characters-in-echo-output/#findComment-1597389 Share on other sites More sharing options...
maxxd Posted June 17, 2022 Share Posted June 17, 2022 If you're specifically looking for a title attribute and not the post body itself, I'd look at adding a maxlength attribute to your input field. You'll want to validate and, if necessary, truncate the text on the server side as suggested by Barand (or just reject the input wholesale) because there are ways around the browser, but it's a start. Admittedly from what I see the title doesn't seem to be the issue in the screencap you posted, so I'll say if I'm misunderstanding what you're calling things and you're referring to what is typically the post body you'll probably want the WP-specific the_excerpt function. Quote Link to comment https://forums.phpfreaks.com/topic/314931-trim-or-limit-characters-in-echo-output/#findComment-1597390 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.