1

我正在为库存列表制作表单文本输入框。我在表单中输入所有内容,然后将所有内容分解为 SQL(尚未在 SQL 部分)。

输入看起来像这样

5-1 1/2 黑色 90° sch 40(有 10 个 sch 80 的)

数量 - 尺寸 - 配件名称(评论)

我把所有东西都放在了他们自己的变量中,现在我只需要从配件名称中删除“(评论)”部分。str_replace 应该可以解决问题,但它似乎只在“某些时候”起作用?我不确定为什么它并不总是有效。下图显示它只工作了 2 次。至于我的正则表达式,我真的不擅长它们。大声笑感谢您对我的小问题的任何帮助。 在此处输入图像描述

foreach ($components as $value) {
    $value=stripslashes($value);
            // get rid of empty lines       
             if  (empty($value)) { continue; }
            // will Split the amount of fittings from the value.
            $quantity = explode("-", $value);
            //Will split the name of the fitting and also give the size of the fittings.
            $test=preg_split('/([a-w]|[A-W])/', $quantity[1],2, PREG_SPLIT_DELIM_CAPTURE);
            //split the comments from the fitting name.
            $comments = explode("(", $test[2]);
            //removes the remaining ) from the left side
            $comments =preg_replace("/\)/", "" , $comments[1]);
 
                    if(!empty($comments)) {
                        $fitting_name = str_replace("($comments)","", $test[1].$test[2]);
                        }else{
                        $fitting_name = $test[1].$test[2];
                        }
        // The table format is just to make sure everything is working right before inputting into SQL
 echo "
  <tr>
    <td>$value</td>
    <td>". $quantity[0] ."</td>
    <td>$test[0]</td>
    <td>$fitting_name</td>
    
    <td>$comments  </td>
  </tr>
  <tr>

";          
        
}
  echo "</tr>
</table>";
}     

输入

0-1/2 PP union 
1- 1 1/2x1 1/4 copper PP fitting red
9- 1 1/2" copper PP 90°
5-2" copper PP 90°    (have 10 of the sch 80 ones) 
13-2" copper PP Tee (have 10 of the sch 80 ones) 
10-1*1*3/4 copper PP tee


60- 3/4"  PP cap and chain value 
50 - 3/4" PP value (we only have 4 more)
19- 3/4" threaded cap and chain value 
0-2" threaded value 
0- 2 1/2" threaded value (have 10 of the sch 80 ones) 


5- 3/4" black Street 90°
0 - 3/4 black union 
0- 1" black union 
0-1" black tee 
1 - 1 1/2 black union 
6-1 1/4 black cap
7-1 1/2" * 1" black bushing
3 - 1 1/2 black coupling 
5-1 1/2 black 90° sch 40? (have 10 of the sch 80 ones) 



4 - 3/8" rod 6'
4-3/8" rod 10'
6 - 1/2" rod 6'
0-5/8" rod 6"
0-5/8" rod 10'
0-3/4 rod 6'
2 - 1" rod 6'(have 10 of the sch 80 ones) 
4

2 回答 2

2

请尝试更改以下行

$fitting_name = str_replace("($comments)","", $test[1].$test[2]);

$fitting_name = str_replace("(" . trim($comments).")","", $test[1].$test[2]);

看看效果。请让我们知道结果。

于 2020-12-14T08:49:11.427 回答
2

在进行任何解析之前,我会首先删除评论,以清理输入。

您可以使用 RegEx 提取它:

\s*\(.*?\)

\s*匹配 0 个或多个空格

\(匹配括号

.*?匹配任何字符(惰性匹配)

\)匹配括号

现在,您可以将其替换为空字符串:

<?php
$input = [
    "0-1/2 PP union ",
    "1- 1 1/2x1 1/4 copper PP fitting red",
    "9- 1 1/2\" copper PP 90°",
    "5-2\" copper PP 90°    (have 10 of the sch 80 ones) ",
    "13-2\" copper PP Tee (have 10 of the sch 80 ones) ",
    "10-1*1*3/4 copper PP tee",
    "",
    "",
    "60- 3/4\"  PP cap and chain value ",
    "50 - 3/4\" PP value (we only have 4 more)",
    "19- 3/4\" threaded cap and chain value ",
    "0-2\" threaded value ",
    "0- 2 1/2\" threaded value (have 10 of the sch 80 ones) ",
    "",
    "",
    "5- 3/4\" black Street 90°",
    "0 - 3/4 black union ",
    "0- 1\" black union ",
    "0-1\" black tee ",
    "1 - 1 1/2 black union ",
    "6-1 1/4 black cap",
    "7-1 1/2\" * 1\" black bushing",
    "3 - 1 1/2 black coupling ",
    "5-1 1/2 black 90° sch 40? (have 10 of the sch 80 ones) ",
    "",
    "",
    "",
    "4 - 3/8\" rod 6'",
    "4-3/8\" rod 10'",
    "6 - 1/2\" rod 6'",
    "0-5/8\" rod 6\"",
    "0-5/8\" rod 10'",
    "0-3/4 rod 6'",
    "2 - 1\" rod 6'(have 10 of the sch 80 ones) "
];

foreach ($input as $value)
{
    $newValue = preg_replace("#\s*\(.*?\)#", "", $value);
    echo $newValue . PHP_EOL;
}

输出 :

0-1/2 PP union 
1- 1 1/2x1 1/4 copper PP fitting red
9- 1 1/2" copper PP 90°
5-2" copper PP 90° 
13-2" copper PP Tee 
10-1*1*3/4 copper PP tee


60- 3/4"  PP cap and chain value 
50 - 3/4" PP value
19- 3/4" threaded cap and chain value 
0-2" threaded value 
0- 2 1/2" threaded value 


5- 3/4" black Street 90°
0 - 3/4 black union 
0- 1" black union 
0-1" black tee 
1 - 1 1/2 black union 
6-1 1/4 black cap
7-1 1/2" * 1" black bushing
3 - 1 1/2 black coupling 
5-1 1/2 black 90° sch 40? 



4 - 3/8" rod 6'
4-3/8" rod 10'
6 - 1/2" rod 6'
0-5/8" rod 6"
0-5/8" rod 10'
0-3/4 rod 6'
2 - 1" rod 6' 

自己试试

于 2020-12-14T08:49:32.297 回答