计算字符串长度并根据字符数收费的PHP函数


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

我有一个OpenCart VQMod,它目前按字符计算字符串长度和费用。它工作得很好,但我需要它按照以下规则收费:

30-45个字符:$8.50

46个以上字符:$12.00

编辑:到目前为止,这个mod将字符串长度与每个字符的固定价格相乘,但我需要它对30-45个字符只收取8.50美元的固定费用,对46+个字符只收费12美元。有人能帮我修改下面的PHP吗?我正在将整个文件粘贴到这里。非常感谢您到目前为止的回复。我真的很感谢社区的帮助。

编辑2:删除了不必要的代码,只显示字符串长度部分。

                    //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;
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;
    } 
}

首先找出哪个数字最大。在这种情况下是45。

$price = 8.50;
for(i=1;i<45;i--){
   echo i - $price.'<br/>';
   if(i < $price){
       break;
    }     
}