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

PHP to trim first few words?

Started by whitsolltd on 31 Jan 2013, 9:47 PM · Views: 1,102

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,511
Plugin Contributions:
176

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

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

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

NOTE: We are sorry that Rod is no longer with us.
We are grateful for all his contributions to the Zen Cart community.

Ozpost - The Ultimate Shipping module for Australian Merchants. Click these links for its Homepage, or Download from Zen-Cart.com.

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

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,511
Plugin Contributions:
176

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.

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

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

NOTE: We are sorry that Rod is no longer with us.
We are grateful for all his contributions to the Zen Cart community.

Ozpost - The Ultimate Shipping module for Australian Merchants. Click these links for its Homepage, or Download from Zen-Cart.com.

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.