Shorten text with preg_replace()


english 6763

Shorten text with preg_replace()

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.

Average overall rating
 off 1 Rating
Awarded 1 x 5  Stars
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?
Please enter the verification code!
Please enter the verification code!
Information(1)
General information
 Free advertising
Post counter
Current information status:
Categories: 9
Posts: 104
Support pages: 57
Downloads for members: 104
Download counter: 646
Reviews total79
Companies: 34
Support: 38
Information: 7