|
You last visited: Today at 23:04
Advertisement
Explode im Foreach
Discussion on Explode im Foreach within the Web Development forum part of the Coders Den category.
06/21/2014, 01:57
|
#1
|
elite*gold: 456
Join Date: Apr 2014
Posts: 243
Received Thanks: 27
|
Explode im Foreach
Hi,
habe leider wieder ein Problem bei meiner zukünftigen Webseite.
Ich möchte eine Userliste die zeilenweise im Format (user:pw) aus einer Textarea an PHP übergeben und schließlich zeilenweise auslesen.
Hier mein PHP Code:
Code:
<?php
if (isset ($_POST['accountData'])) {
//Die Textarea nach jeder Zeile trennen und in einem Arrayfeld speichern
$msg = explode( "\r\n", $_POST['accountData'] );
foreach( $msg as $data )
list($user, $pass) = explode(":", $data);
echo "User: $user , Passwort: $pass <br>";
}
?>
Problem ist, dass immer nur der letzte Datensatz der Textarea ausgegeben wird. Wo liegt mein Fehler ?
Danke im voraus!
|
|
|
06/21/2014, 02:27
|
#2
|
elite*gold: 1329
Join Date: Jun 2009
Posts: 1,873
Received Thanks: 960
|
Bist du dir bei dem Trennzeichen sicher? Schau dir deine Liste mal mit nem Hex Editor an.
|
|
|
06/21/2014, 02:38
|
#3
|
elite*gold: 456
Join Date: Apr 2014
Posts: 243
Received Thanks: 27
|
Quote:
Originally Posted by マルコ
Bist du dir bei dem Trennzeichen sicher? Schau dir deine Liste mal mit nem Hex Editor an.
|
Meinst du den Doppelpunkt oder den Zeilenumbruch? Die zwei Wörter sind definitiv durch einen Doppelpunkt getrennt.
Ich habe vorher mal die Zeilenweise Ausgabe getestet und das hat geklappt. Also ich konnte mit folgendem Code in jeder Zeile "user:passwort" der jeweiligen Zeile ausgeben.
Code:
<?php
if (isset ($_POST['accountData'])) {
$msg = explode( "\r\n", $_POST['accountData'] );
foreach( $msg as $data )
echo "$data <br>";
}
?>
|
|
|
06/21/2014, 13:44
|
#4
|
elite*gold: 1329
Join Date: Jun 2009
Posts: 1,873
Received Thanks: 960
|
Ich mein schon den Zeilenumbruch. Darüber gehts doch in dem Thread, oder?
|
|
|
06/21/2014, 14:34
|
#5
|
elite*gold: 456
Join Date: Apr 2014
Posts: 243
Received Thanks: 27
|
Der funktioniert aufjedenfall. Siehe mein Post darüber, die zeilenweise Ausgabe bekomme ich hin. Hier nochmal zur Verdeutlichung. Die Eingabe sieht z.b. so aus:
Username1:Passwort1
Username2:Passwort2
Username3:Passwort3
Username4:Passwort4
Ich möchte nun jeden User mit seinem entsprechenden Passwort in eine MySQL DB speichern, beziehungsweise zum Testen erstmal per echo einzeln ausgeben können.
|
|
|
06/22/2014, 14:42
|
#6
|
elite*gold: 456
Join Date: Apr 2014
Posts: 243
Received Thanks: 27
|
Push - Problem besteht immernoch!
|
|
|
06/22/2014, 16:09
|
#7
|
elite*gold: 20
Join Date: Aug 2005
Posts: 652
Received Thanks: 189
|
Führe das mal aus:
PHP Code:
<form method="POST">
<textarea name="accountData"></textarea>
<input type="submit"></input>
</form>
<?php
if (isset ($_POST['accountData'])) {
$msg = explode( "\r\n", $_POST['accountData'] );
print_r($msg);
echo "<h4>Original</h4>";
foreach( $msg as $data )
list($user, $pass) = explode(":", $data);
echo "User: $user , Passwort: $pass <br>";
}
?>
<hr>
<?php
if (isset ($_POST['accountData']))
{
$data = $_POST['accountData'];
preg_match_all("/[\w]+:[\w]+/", $data, $matches);
print_r($matches[0]);
echo "<h4>Method1</h4>";
foreach($matches[0] as $data) {
list($user, $pass) = explode(":", $data);
echo "User: $user , Passwort: $pass <br>";
}
}
?>
<hr>
<?php
if (isset ($_POST['accountData']))
{
$msg = explode("\r\n", $_POST['accountData']);
print_r($msg);
echo "<h4>Method2</h4>";
for($i = 0; $i < sizeof($msg); $i++) {
list($user, $pass) = explode(":", $msg[$i]);
echo "User: $user , Passwort: $pass <br>";
}
}
?>
Bei allen 3 Methoden ist der Array exakt gleich. Verwundert deswegen natürlich warum deine Variante nicht funktioniert.
|
|
|
06/22/2014, 16:13
|
#8
|
elite*gold: 456
Join Date: Apr 2014
Posts: 243
Received Thanks: 27
|
Perfekt! Beide Methoden haben funktioniert, das Original allerdings nicht. Wo liegt der Fehler ??
Danke aufjedenfall schon einmal!!
|
|
|
 |
Similar Threads
|
Finding a specific name from a list using a Foreach check
10/05/2013 - CO2 Private Server - 8 Replies
I've been working on a new event idea which involves 2 people being paired up and then parts of each char being checked against the other, I have everything working fine apart from 1 of the checks that selects the players opponent from the list by checking the names in the list, It only selects the first person added to the list and then returns a fail rather than checking the whole list and returning the correct match if they are still there, I have checked and players are being added fine and...
|
Mit Foreach mehrere Daten in MySQL eintragen^
07/12/2012 - Web Development - 4 Replies
Hallöchen community,
komme gerade nicht weiter, steh irgendwie aufm Schlauch..
Ich habe folgendes:
$xml = @simplexml_load_file($xmllink);
foreach($xml->newsitems->newsitem as $news) {
$title = $news->title; // Titel der News
$url = $news->url; // Link zu den News
|
Direct a foreach at Char in Map?
04/01/2010 - CO2 Private Server - 8 Replies
I've been working at a hunt quest that teleports chars to a map where they must kill a monster get a key find the treasure box, open it then teleport back to TC.
This is what I have but it directs the tele to all characters in game
foreach (Game.Character C in Game.World.H_Chars.Values)
{
Game.World.SendMsgToAll("SYSTEM", "The Treasure has been found. Better luck next time.", 2011, 0);
...
|
All times are GMT +1. The time now is 23:06.
|
|