Search code examples
phpcountcharacteropencartstrlen

PHP Function that Counts String Length and Charges Price Based on # of Characters


I've got an OpenCart VQMod that currently counts string length and charges by character. It works perfectly, but I need it to charge with the following rules:

30-45 characters: $8.50

46+ characters: $12.00

Edit: As of now, this mod multiplies the string length with a set price per character, but I need it to only charge a flat $8.50 for 30-45 characters, or $12 for 46+ characters. Can anyone help me modify the following PHP? I'm pasting the entire file here. Thanks so much for your responses so far. I really appreciate the help of the community.

Edit 2: Removed unnecessary code, only showing string length potion.

                    //Q: Option Price By Character
                    $optprice = '';
                    $optprefix = '';
                    if ($option_query->row['type'] == 'text' || $option_query->row['type'] == 'textarea') {
                            if (strlen($option_value)) {
                                $optprice = (strlen($option_value) * $option_query->row['price_per_char']);
                                $optprefix = '+';
                                $option_price += $optprice;

Solution

  • if ($option_query->row['type'] == 'text' || $option_query->row['type'] == 'textarea') {
        if (strlen($option_value)) {
            // LumberJack's new code
            $string_length = strlen($option_value);
            if($string_length >= 30 && $string_length <= 45) 
            { $optprice = 8.5; }
            else if($string_length >= 46)
            { $optprice = 12.00; }
            else {
            // end my new code
              $optprice = (strlen($option_value) * $option_query->row['price_per_char']);
            } // I moved this up two lines
            $optprefix = '+';
            $option_price += $optprice;
        } 
    }