Zen Cart Logo
Forums / Templates, Stylesheets, Page Layout / PHP to trim first few words?

PHP to trim first few words?

Views: 1,098

Results 1 to 8 of 8
31 Jan 2013, 9:47 PM
#1
whitsolltd avatar

whitsolltd

New Zenner

Join Date:
Sep 2012
Posts:
23
Plugin Contributions:
0

PHP to trim first few words?

Hi All

I'm wondering if this may be possible.

On our website product listing (example here), I would like to remove the words 'Product Description:' This is currently been written as part of the description and I would like it to still show on the product info page.

I've been looking at the php trim function but seem to be getting something wrong.

Anybody have any ideas how this can be achieved?

Thanks in advance

31 Jan 2013, 10:53 PM
#2
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
177

Re: PHP to trim first few words?

Better to use substr($variable, 5) where 5 is the number of characters you want it to skip from the beginning

1 Feb 2013, 2:14 PM
#3
whitsolltd avatar

whitsolltd

New Zenner

Join Date:
Sep 2012
Posts:
23
Plugin Contributions:
0

Re: PHP to trim first few words?

Thanks for this.

I assume that this needs to go in the product_listing.php file.

I currently have the following code that, as far as I'm aware, inserts the description:

$lc_text = '<h3 class="itemTitle"><a href="' . zen_href_link(zen_get_info_page($listing->fields['products_id']), 'cPath=' . (($_GET['manufacturers_id'] > 0 and $_GET['filter_id'] > 0) ? zen_get_generated_category_path_rev($_GET['filter_id']) : ($_GET['cPath'] > 0 ? zen_get_generated_category_path_rev($_GET['cPath']) : zen_get_generated_category_path_rev($listing->fields['master_categories_id']))) . '&products_id=' . $listing->fields['products_id']) . '">' . $listing->fields['products_name'] . '</a></h3><div class="listingDescription">' . zen_trunc_string(zen_clean_html(stripslashes(zen_get_products_description($listing->fields['products_id'], $_SESSION['languages_id']))), PRODUCT_LIST_DESCRIPTION) . '</div>';

Could you be so kind as to advise where this command should be inserted?

Thanks again

1 Feb 2013, 2:45 PM
#4
whitsolltd avatar

whitsolltd

New Zenner

Join Date:
Sep 2012
Posts:
23
Plugin Contributions:
0

Re: PHP to trim first few words?

Sorted.

I have ended up with this:

    $lc_text = '<h3 class="itemTitle"><a href="' . zen_href_link(zen_get_info_page($listing->fields['products_id']), 'cPath=' . (($_GET['manufacturers_id'] > 0 and $_GET['filter_id'] > 0) ?  zen_get_generated_category_path_rev($_GET['filter_id']) : ($_GET['cPath'] > 0 ? zen_get_generated_category_path_rev($_GET['cPath']) : zen_get_generated_category_path_rev($listing->fields['master_categories_id']))) . '&products_id=' . $listing->fields['products_id']) . '">' . $listing->fields['products_name'] . '</a></h3><div class="listingDescription">' . zen_trunc_string(zen_clean_html(substr(stripslashes(zen_get_products_description($listing->fields['products_id'], $_SESSION['languages_id'])),24)), PRODUCT_LIST_DESCRIPTION) . '</div>';

Hopefully this is an efficent method. Either way it does seem to be working.

Thanks again for the help

2 Feb 2013, 5:31 AM
#5
rodg avatar

rodg

Deceased

Join Date:
Jan 2007
Location:
Australia
Posts:
6,263
Plugin Contributions:
4

Re: PHP to trim first few words?

whitsolltd:

), 'cPath=' . (($_GET['manufacturers_id'] > 0 and $_GET['filter_id'] > 0) ? zen_get_generated_category_path_rev($_GET['filter_id']) : ($_GET['cPath'] > 0 ? zen_get_generated_category_path_rev($_GET['cPath']) : zen_get_generated_category_path_rev($listing->fields['master_categories_id']))) . '&products_id=' . $listing->fields['products_id']) . '">' . $listing->fields['products_name'] . '</a></h3><div class="listingDescription">' . zen_trunc_string(zen_clean_html(substr(stripslashes(zen_get_products_description($listing->fields['products_id'], $_SESSION['languages_id'])),24)), PRODUCT_LIST_DESCRIPTION) . '</div>';

Hopefully this is an efficent method. Either way it does seem to be working.

Thanks again for the help

WARNING: This code is susceptible to hacking. You should never use GET or POST variables without sanitizing them first.

Cheers
Rod

2 Feb 2013, 5:45 AM
#6
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
177

Re: PHP to trim first few words?

RodG:

You should never use GET or POST variables without sanitizing them first.
While that statement is true, in the context of the code you quoted, the sanitization has already been taken care of for those specific variables.

2 Feb 2013, 5:56 AM
#7
rodg avatar

rodg

Deceased

Join Date:
Jan 2007
Location:
Australia
Posts:
6,263
Plugin Contributions:
4

Re: PHP to trim first few words?

DrByte:

While that statement is true, in the context of the code you quoted, the sanitization has already been taken care of for those specific variables.

Thanks. It isn't possible to tell from just looking at the code snippet. I figured it best to be safe than sorry. Besides, it never hurts to repeat information about safe coding practices. :)

Cheers
Rod

2 Feb 2013, 9:20 AM
#8
whitsolltd avatar

whitsolltd

New Zenner

Join Date:
Sep 2012
Posts:
23
Plugin Contributions:
0

Re: PHP to trim first few words?

This is good to know anyway. I haven't changed that element of the code but this will be worth consideration in future.

Thanks for all the help.