0

I want to cut description string "$values['description']" after 13 character and add 3 dots after.

I found a solution here:

PHP - cut a string after X characters

$string = (strlen($string) > 13) ? substr($string,0,10).'...' : $string;

But it's a little bit complicated for me to combinate it with my code:

'desc' => str_replace(array("\t","\r", "\n"), '', preg_replace('/(<.*?>)/', '', htmlspecialchars_decode($values['description'])))

Community
  • 1
  • 1
Adrian
  • 2,576
  • 9
  • 49
  • 97

1 Answers1

2
$string = str_replace(array("\t","\r", "\n"), '', preg_replace('/(<.*?>)/', '', htmlspecialchars_decode($values['description'])));

$string = (strlen($string) > 13) ? substr($string,0,10).'...' : $string;

Then add to your array?... leaves you with:

'desc' => $string

So the code should follow lines 1 and 2. Then $var= array('desc' => $string); or whatever your trying to do.

Shawn
  • 3,583
  • 8
  • 46
  • 63
  • The first code snippet ($string = ...) is from that another question. My code is the " 'desc' => str_replace " <- I want to combine this code with that first one. – Adrian Nov 29 '12 at 23:05
  • This has to be two commands. One is doing a replace. Once that is done we know something concrete about our string (i.e the size). Then using the logic you wanted applied to the string. – Shawn Nov 29 '12 at 23:05
  • @Adrian read my last comment. We need to do this in two lines. Do the change before your code and then your code would look like this: 'desc' => $string. I'll update the answer in a moment. – Shawn Nov 29 '12 at 23:06
  • If this worked as a solution for you please give it the check so others know who find this post at a later date. If not ask a question and I will respond to clarify more. Thanks! – Shawn Nov 29 '12 at 23:10
  • So in your case. Add lines 1 and 2 right before $data[$key] = array(...). Then inside the $data[$key] = array( ... 'desc' => $string); – Shawn Nov 29 '12 at 23:13