Shorten text with preg_replace()


english 2862
If you want to shorten your description text while removing any unwanted special characters, there is an efficient way to do it.
This is advantageous in product_listing.php, for example.

The shortened text is not cut off in the middle of the word, but only afterwards.

Check whether the following section is defined in the sql query:

SUBSTRING_INDEX(pd.products_description, ' ', '10') as products_description,

Change this so that the base query looks like this:

pd.products_description,

Then insert this code sequence:

    $originaldescriptionstring = $variable['products_description']; 
    $outputdescriptionstring = preg_replace ('/<[^>]*>/', '', $originaldescriptionstring);
    $dsc_string	= $outputdescriptionstring;
    $dsc_length	= '45';
    $dsc_output	= preg_replace( '/[^ ]*$/', '', substr( $dsc_string, 0, $dsc_length ) ) . ' ...';

At the point of the issued product description:
Example:

 . $variable['products_description'] . 

replace with the following:

 . $dsc_output . 

or

<?php echo $variable['products_description'];?>

replace with the following:

<?php echo $dsc_output;?>

The text is shortened to 45 letters, all special characters are removed and the last word is not cut off in the middle.

Why your email address?
If necessary, I will contact you to help you with the implementation.
Your email address will not be made public.
Is this post helpful to you?
Average overall rating
 off 1 Rating
Awarded 1 x 5  Stars
Information(2)
General information
 Free advertising
 Lazy Loading
Post counter
Current information status:
Categories: 8
Posts: 104
Support pages: 55
Downloads for members: 104
Download counter: 646
Reviews total58
Companies: 26
Support: 25
Information: 7