0
votes

Is it possible to slice text from custom post type in WordPress to parts after X characters and put it to different div in the loop? For example i have post with this content:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam nisl libero, consequat vel augue porta, pharetra convallis elit. Etiam pharetra tempor libero, non viverra diam pellentesque ac. Nunc aliquet, elit maximus aliquam dignissim, urna sem porta odio, sit amet aliquet turpis est at ipsum.

And i want to slice it on parts with 50 characters max, so I want to have code like this:

<div>Lorem ipsum dolor sit amet, consectetur adipiscing</div>
<div> elit. Nullam nisl libero, consequat vel augue port</div>
<div>next 50 chars...</div>
<div>next 50 chars...</div>
1
Take a look at str_split(). Easiest php solution of dividing string by length into several smaller strings(array of strings). You can iterate over the array and add <div>...</div> separatly for ech element. Or something along the lines of that.Eugene Anisiutkin

1 Answers

1
votes
<?php
$string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam nisl libero, consequat vel augue porta, pharetra convallis elit. Etiam pharetra tempor libero, non viverra diam pellentesque ac. Nunc aliquet, elit maximus aliquam dignissim, urna sem porta odio, sit amet aliquet turpis est at ipsum.";
$outputs = str_split($string, 50);
foreach ($outputs as $output) {
    echo "<div>$output</div>";
}

The above code will show you how to slice text after every 50 characters and generate using that. You have to take that post text and slice it like this.