Php Truncate String

  1. The input string. An array of string s can be provided, in which case the replacements will occur on each string in turn. In this case, the replace, offset and length parameters may be provided either as scalar values to be applied to each input string in turn, or as array s, in which case the corresponding array element will be used for each input string.
  2. D ello world lo world orld d ello world lo world orld. Run ยป Result Size: 497 x 420.
  3. The input string. An array of string s can be provided, in which case the replacements will occur on each string in turn. In this case, the replace, offset and length parameters may be provided either as scalar values to be applied to each input string in turn, or as array s, in which case the corresponding array element will be used for each input string.
  4. PHP truncate string - cut string after certain length - output shorter string.php - PHP truncate string - cut string after certain length - output shorter string.php.

Hi, I have this nice function to truncate a string with HTML.
It's very good only now I ran into a problem.

How to truncate part of a string in php. Get String Based on a Certain Length Without Cutting It. Set character limit of PHP echoed item from JSON within loop. How can I prevent SQL injection in PHP? Enumerations on PHP. Deleting an element from an array in PHP.

Truncate

There are cases where for example The input will be this:
<a href='some url'>this is a link</a>
and what I want to output is exactly the same, even if it it's bigger than the maximum length specified.

In other words, inside html tags it can't cut.

Can someone help me, please?

this is the function I have:

Editedby antonioazevedo
  • 4 Contributors
  • forum7 Replies
  • 1,381 Views
  • 2 Days Discussion Span
  • commentLatest PostLatest Postby diafol
Length

Recommended Answers

You could do an extra check. If you strip_tags the input and count it. If it's shorter then you specified you can just return the input as result. Can you post a test script, with input and expected output?

Php Truncate StringJump to Post

just did this quickly with strpos, will this work for you?

Jump to Post

All 7 Replies

Php truncate string after x charactersString

Php Cut String At Character

pritaeas2,045Most Valuable Poster ModeratorFeatured Poster

Php Truncate String After Character

You could do an extra check. If you strip_tags the input and count it. If it's shorter then you specified you can just return the input as result. Can you post a test script, with input and expected output?

Php Truncate String

Editedby pritaeas because:Edit