Reputation: 29
I'm making a form text input box for an inventory list. I type everything into the form and it breaks everything down to be put into SQL(not at the SQL part yet).
Input would look something like this
5-1 1/2 black 90° sch 40 (have 10 of the sch 80 ones)
amount - size - fitting name ( comments)
I got everything in their own variable now I just need to remove the "(comments)" part from the fitting name. str_replace should do the trick but it seems to only work "some times" ? I'm not sure why it doesn't always work. The picture below show that it only worked 2 times. As for my regex, I'm really bad at them. lol Thanks for any help you can give with my little problem.
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>";
}
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)
Upvotes: 1
Views: 61
Reputation: 15247
I would first remove the comment before doing any parsing, to cleanse the input.
You can extract it with a RegEx :
\s*\(.*?\)
\s*
matches 0 or more white spaces
\(
matches a parenthesis
.*?
matches any characters (lazy match)
\)
matches a parenthesis
Now, you can replace this by an empty string :
<?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;
}
Output :
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'
Upvotes: 2
Reputation: 8043
Plese try changing the following line
$fitting_name = str_replace("($comments)","", $test[1].$test[2]);
to
$fitting_name = str_replace("(" . trim($comments).")","", $test[1].$test[2]);
and see the effect. Please let us know the result.
Upvotes: 2