How To Trim A String

Table of contents:

How To Trim A String
How To Trim A String

Video: How To Trim A String

Video: How To Trim A String
Video: How to STRING TRIM like a PRO 😎😎😎 2024, March
Anonim

Many web designers use the PHP scripting language - Personal Home Page Tools. This is due to the support of web applications by many hosting providers. Quite often, site pages contain strings - a sequence of different characters: spaces, numeric values, letters, etc. Due to various circumstances, their trimming is required.

How to trim a string
How to trim a string

It is necessary

a computer with installed software products for creating web applications

Instructions

Step 1

Determine if you need to trim a string in PHP. As a rule, these are news messages for which a preview of one or two lines of the whole message is created on the main page.

Step 2

Select a piece of content on the basis of which the preview will be created. Use the built-in string processing functions, in particular the truncation command, substr (). Specify line ending options - ellipsis or whole word.

Step 3

Adjust the trimming parameters you specify: determine the length of the line and specify the preservation of the last word in the line as a whole.

Step 4

Split the string at the space separator, thereby forming an array of words. Loop through the array of words, adding all the words to the new array.

Step 5

At each iteration of the loop, measure the total length using the lines of the generated word array. Add a new word to the previous loop.

Step 6

When the total amount of the string length reaches or exceeds the specified value, the cycle stops. The last word added to the array is considered final. The result is a string that matches the specified length and consists of characters and words, separated by spaces.

Recommended: