S
S
santavits2018-06-08 14:53:53
PHP
santavits, 2018-06-08 14:53:53

How to process lines in textarea with ":" delimiter?

The essence of the problem
There is a form with textarea

<form action="" method="POST">
<textarea rows="10" cols="45" style="resize: none;height: 120px;width: 540px;" name="aks"><?php echo isset($_POST['aks']) ? htmlspecialchars($_POST['aks']) : ''; ?></textarea/>
<input type="submit"id="onetime" class="act1" value="Добавить">
</form>

There I drive in the text
login1:pass1
login2:pass2
login3:pass3
After that I want this text to be added to the database in the login field of the value login1,login2,login3
and in the password field of the value pass1,pass2,pass3
all values ​​from a new line
to process the button used
if (isset($_POST['text'])){
  
  
$aks = explode("\n", $_POST['aks']);
 
foreach ($aks as $ak) {
    mysql_query("INSERT INTO `vanek` SET aks='{$ak}'");
  
}	
}

but this adds each new line with textarea
how to make text dilute with ":" delimiter

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ilya, 2018-06-08
@santavits

<?php
$users = <<<HTML
login1:pass1
login2:pass2
login3:pass3
HTML;

foreach (explode(PHP_EOL, $users) as $user) {
    $user_data = explode(':', trim($user));
    $login = $user_data[0];
    $password = $user_data[1];
}

D
Denis, 2018-06-08
@notwrite

Something like Php split

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question