Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 标记字符串和检查数字_Php - Fatal编程技术网

Php 标记字符串和检查数字

Php 标记字符串和检查数字,php,Php,所以我对PHP真的很陌生——就像这是我第一次深入了解它一样 我所做的是尝试获取从表单传入的字符串,并用分隔符对其进行解析。我目前正在使用“-”作为分隔符。如果输入了我要查找的项目,其中一个项目将有一个整数值 基本上,我要查找的有效字符串的形式如下: -数量-库存单位-标题-价格 保证填写SKU、标题和价格,但不填写数量。 这样我就可以收到格式为 -20-SKU:5563产品A-价格:$19.95 或者 -SKU:5563产品A价格:$19.95 如果特定产品没有实际订购,即没有实际订购数量。 这

所以我对PHP真的很陌生——就像这是我第一次深入了解它一样

我所做的是尝试获取从表单传入的字符串,并用分隔符对其进行解析。我目前正在使用“-”作为分隔符。如果输入了我要查找的项目,其中一个项目将有一个整数值

基本上,我要查找的有效字符串的形式如下:

-数量-库存单位-标题-价格

保证填写SKU、标题和价格,但不填写数量。 这样我就可以收到格式为

-20-SKU:5563产品A-价格:$19.95

或者

-SKU:5563产品A价格:$19.95

如果特定产品没有实际订购,即没有实际订购数量。 这些字符串也是以串联方式传递的,因此我尝试使用“-”作为delimeter进行标记,如果遇到一个数字,而该数字只有字符串的数量部分应该匹配,我将返回一个包含所需数据的字符串

问题是ctype_数字的计算结果永远不会为真

public function display_entry_cp ($data)
{
    $process_product = explode("-", $data);
    $return_array = "Products ordered:\n";
    //print_r($process_product);
    for ($i = 0; $i < count($process_product); ++$i) {
        // echo($process_product[$i] . "<br/>");
        if($process_product[$i] != '' && ctype_digit($process_product[$i])){
            echo("Element is digit!");
            // look for the first int seen, that is our quantity, next is SKU, Title then Price
            $ordered_product = 'Quantity: ' . $process_product[$i] . ' ' . $process_product[++$i] . ' ' . $process_product[++$i] . ' ' . $process_product[++$i];
            $return_array = $ordered_product;
        }
    }
    return $return_array;
}
在本例中,返回的字符串如下所示:

-133-SKU:1101说明:产品A:$8.95-SKU:1200说明:产品B-价格:$9.50-SKU:1105说明:产品C-价格:$17.95-133-SKU:910说明:产品D:$19.95


在本例中,我只想返回第一项和最后一项

我看了很久。作为示例提供的数据与规范不匹配-也就是说,在记录的开头应该有两个相邻的连字符,没有数量,但您的数据只有一个。您的代码没有处理这一点,因此后续标记不是预期的标记,因此您的条件失败

如果您添加了缺少的连字符,那么这段代码可以正常工作

有一个不同的问题。在组装$order\u产品字符串时会出现另一个问题。在每个术语上递增循环索引变量。这是一种不错的做法,但实际上是一种糟糕的做法,因为它以后会让人感到困惑,除非在有短记录的地方,索引的增量超过了数组中的元素数

程序员使用字段和记录分隔符有一个原因:它使可靠地处理这种差异成为可能。已经有了一个很好的格式:CSV是很好理解和可靠的

但是,你说这些数据来自一个表单。如果是这样的话,它一定出现在$\u GET或$\u POST变量的某个地方。不知何故,您可能已经将数据混合成这种专有格式,以便在其上运行此例程。我不得不问:为什么


这是一个典型的xy问题。你问了一件事,但你真的应该问别的。在这种情况下,我猜,PHP中的表单处理,所以我想我已经想出了一个解决方案-因为我不是专家,我想知道这是否比我上面的方法更好

public function display_entry_cp ($data)
{

    /*
    *   Precondition: String comes in the format:
    *   "SKU/DESCR/PRICE/QTY,SKU/DESCR/PRICE/,SKU/DESCR/PRICE/QTY," ...etc
    *   QTY may or may not be an empty string - this is our indicator that item was ordered
    */ 

    // First get all the products into single lines we can work with
    $process_product = explode(",", $data);
    // String to hold our return value
    $return_str = "Products ordered:";
    // Loop through and process the extracted strings
    foreach($process_product as $value) {
        // Testing
        //echo($value . "<br/>");
        try{
            // Split the string by the "/" delimiter
            $explode = explode("/", $value);
            // Count should always be 4, even if no QTY entered, it will contain " "
            if(count($explode) == 4){
                list($sku, $descr, $price, $qty) = explode("/", $value);
                // Checking if there really is a QTY
                if (trim($qty) !== "") {
                    //echo(" SKU: " . $sku . " Description: " . $descr . " Price: $" . $price . " Qty: " . $qty . "<br/>");
                    $ordered_product = (" SKU: " . $sku . " Description: " . $descr . " Price: $" . $price . " Qty: " . $qty);
                    //echo($ordered_product);
                    $return_str .= $ordered_product;
                }
            }
        }
        catch (Exception $e){
            log_message('error', $e->getMessage()); 
            return;
        } // end try/catch
    }// end foreach

// Final check to see if return_str is what we expect
//echo($return_str);
return $return_str;
}
如注释中所述,字符串现在的格式如下:

SKU/DESCR/PRICE/QTY、SKU/DESCR/PRICE/、SKU/DESCR/PRICE/QTY等

数量可以是空的,也可以不是空的。无论哪种情况,它都将包含前面的空格字符。 现在我可以用一个逗号来分隔,首先将所有产品划分为一个自己的字符串。从那里,我可以进一步将字符串拆分为单独的组件。这也使我能够使用foreach循环,就像使用for循环一样


如果您发现任何问题,请告诉我。

这些数据来自哪里?如果它在其他地方是独立的,那么它应该保持独立,而不是随意串成一个字符串。想想如果产品名称自然包含一个-尝试列表$QTY,$SKU,$DISCR,$Que=爆炸,$DATA,将会发生什么;除了如果$数量{//error here}@Marc B你说得对。我可能应该使用一个更独特的分隔符。@ccKep,谢谢你的建议!我会试试这个,看看我有没有运气你对缺失的连字符是正确的!我已经在原来的帖子中做了调整。那是我的错误。我知道增加计数是不好的,当我这么做的时候我退缩了。在这一点上,我只是想让它工作,然后我可以对它进行优化,使它更健壮。我还将研究CSV格式。我没想过用它作为蓝图。是的,数据确实来自$\u POST,但这是一个从父类继承的类。我无法访问原始数据。它只是抓住所有字段并将其传递。我可以访问$data字段,就是这样。另外,只有一个FWIW,所有的数据都已经被安全地保存起来了,我只是为了显示目的而尝试操作字符串。
public function display_entry_cp ($data)
{

    /*
    *   Precondition: String comes in the format:
    *   "SKU/DESCR/PRICE/QTY,SKU/DESCR/PRICE/,SKU/DESCR/PRICE/QTY," ...etc
    *   QTY may or may not be an empty string - this is our indicator that item was ordered
    */ 

    // First get all the products into single lines we can work with
    $process_product = explode(",", $data);
    // String to hold our return value
    $return_str = "Products ordered:";
    // Loop through and process the extracted strings
    foreach($process_product as $value) {
        // Testing
        //echo($value . "<br/>");
        try{
            // Split the string by the "/" delimiter
            $explode = explode("/", $value);
            // Count should always be 4, even if no QTY entered, it will contain " "
            if(count($explode) == 4){
                list($sku, $descr, $price, $qty) = explode("/", $value);
                // Checking if there really is a QTY
                if (trim($qty) !== "") {
                    //echo(" SKU: " . $sku . " Description: " . $descr . " Price: $" . $price . " Qty: " . $qty . "<br/>");
                    $ordered_product = (" SKU: " . $sku . " Description: " . $descr . " Price: $" . $price . " Qty: " . $qty);
                    //echo($ordered_product);
                    $return_str .= $ordered_product;
                }
            }
        }
        catch (Exception $e){
            log_message('error', $e->getMessage()); 
            return;
        } // end try/catch
    }// end foreach

// Final check to see if return_str is what we expect
//echo($return_str);
return $return_str;
}