Click here to Skip to main content
15,887,376 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
$txt = <<<EOL
    934030913 G 9RUPISOLV 20BUST 00001 P 04
    0055 G 0CIBALGINA DUE FAST*24CPR 200MG 00001 P 01
        EOL;
        
        $specialChars = preg_quote( '#$%^&*()+=-[]\';,./{}|\":<>?~', '#' );
        preg_match_all(
            '#([0-9]{4,9}\s+[A-Z]{1}\s+([' . $specialChars . 'A-Z0-9 ]+)\s+([0-9]{3,7})n?\s+P\s+([0-9]+))#',
            $txt, $match
        );


Array example:

[0] => Array ( [0] => 934030913 G 9RUPISOLV 20BUST 00001 P 04 
                      [1] => 0055 G 0CIBALGINA DUE FAST*24CPR 200MG 00001 P 01 ) 
       [1] => Array ( [0] => 934030913 G 9RUPISOLV 20BUST 00001 P 04 
                      [1] => 0055 G 0CIBALGINA DUE FAST*24CPR 200MG 00001 P 01 ) 
       [2] => Array ( [0] => 9RUPISOLV 20BUST
                      [1] => 0CIBALGINA DUE FAST*24CPR 200MG ) 
       [3] => Array ( [0] => 00001
                      [1] => 00002 ) 
       [4] => Array ( [0] => 04 
                      [1] => 01) )


I need insert match[2][x] match[3][x] match[4][x] (of all results) in a mysql db, i think with foreach but i don't know how!

[2] => Array ( 	[x] => PRODUCT ) in product column    |
    [3] => Array ( 	[x] => QUANTITY) in quantity column   |  ONE RECORD
    [4] => Array ( 	[x] => NUMBER ) in terminal column    |


How i can do that?

What I have tried:

$indexes = array(2, 3, 4); 
	foreach ($indexes as $index) { 
		foreach ($match[$index] as $value) { 
			$sql = "INSERT INTO `table`(`product`) VALUES ('{$value}');"; mysql_query($sql); } }
?>
Posted
Updated 10-Sep-17 9:49am

Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
 
Share this answer
 
Comments
Member 12989102 10-Sep-17 15:46pm    
Don't worry this service is for an internal web app, it's not public!
Why there aren't any solution?
Patrice T 10-Sep-17 16:42pm    
Today is Sunday, things are always slow.
I'm not an PHP programmer, but maybe these links will get you started:
PHP Connect to MySQL[^]
PHP Insert Multiple Records Into MySQL[^]
PHP and MySQL[^]

Here is a foreach example I found on PHP: foreach - Manual[^]
PHP
<?php
$arr = array(1, 2, 3, 4);
foreach ($arr as &$value) {
    $value = $value * 2;
}
// $arr is now array(2, 4, 6, 8)
unset($value); // break the reference with the last element
?>
 
Share this answer
 
v2
Comments
Member 12989102 10-Sep-17 15:50pm    
I know how to use mysql but i don't know how use foreach (php) for my case!
If someone can do it in C# or VB.NET, for me it does the same!
I need that, when a file is edited, through regular expression:
#([0-9]{4,9}\s+[A-Z]{1}\s+([' . $specialChars . 'A-Z0-9 ]+)\s+([0-9]{3,7})n?\s+P\s+([0-9]+))# it search that i want and insert it into a mysql db
Member 12989102 10-Sep-17 16:57pm    
I know (not very well) how foreach work. But i don't know how can I get this:

match [2] [0] + match [3] [0] + match [4] [0]
[2] => Array ([0] => 9RUPISOLV 20BUST) in PRODUCT column +
[3] => Array ([0] => 00001) in QUANTITY column +
[4] => Array ([0] => 04) in TERMINAL column

After that, the cycle will have to continue
match [2] [1] + match [3] [1] + match [4] [1]
[2] => Array ([1] => 0CIBALGINE TWO FAST * 24CPR 200MG)) in PRODUCT column +
[3] => Array ([1] => 00002) in QUANTITY column +
[4] => Array ([1] => 01) in TERMINAL column

etc...
Don't worry this service is for an internal web app, it's not public!
Why there aren't any solution?
 
Share this answer
 
Comments
RickZeeland 10-Sep-17 15:42pm    
CodeProject probably has more C# / VB.NET programmers than PHP programmers, also keep in mind that on sunday they might be doing other things.
But if you do not get any response you might try another site like StackOverflow, good luck !
Member 12989102 10-Sep-17 15:46pm    
i posted on stackoverflow but nobody helped me
https://stackoverflow.com/questions/46143280/how-insert-array-in-a-mysql-db-foreach#comment79248309_46143280

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900