Fileregels inlezen, in array zetten, ontdubbelen en dan bewerking uitvoeren op array
Stel dat het bestand per regel een naam heeft. Deze wil ik dan allemaal inlezen, ontdubbelen en daarna bij de overgebleven regels een boodschap toevoegen en weer opslaan in een bestand.
Graag jullie helprichting.... dank!
http://php.net/manual/en/function.file.php
Je kan dan vervolgens de array sorteren en dan de dubbele regels eruit halen.
Ongeveer zoals dit :
http://adoptive.esy.es/tags/
Met hier de code als text :
http://adoptive.esy.es/tags/tags.txt
sorteren: sort() (of zie alle varianten op http://php.net/manual/en/array.sorting.php )
Dank, ik ga stoeien met jullie tips!
Nadat is vastgesteld dat je het goede gereedschap voor de klus hebt gekozen kunnen we het over de klus zelf hebben.
Dus, wat probeer je op deze manier op te lossen?
Toevoeging op 24/12/2017 12:14:32:
Ik heb nu de volgende code gemaakt. Ik kan dit testen, maar wil graag horen of dit de goede richting is.
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
?>
<?php
if ($_SERVER['REQUEST_METHOD']=="POST") {
$ontdubbelen=$_POST['ontdubbel'];
if ($ontdubbelen=="JA") {
echo '<center>Ontdubbelen is gestart, momentje...';
$lijst="Adressenlijst.txt";
$lines = file($lijst);
$result = array_unique($lines);
file_put_contents($lijst, var_export($result, true));
echo '<br><center><b>De lijst is ontdubbeld.</b><center><br>';
echo '<p align="center"><button onclick="self.close()">Sluit dit venster</button>  ';
die();
} else {
echo '<br><center>Er is iets misgegaan. Bewerking gestopt!</center><br>';
echo '<p align="center"><button onclick="self.close()">Sluit dit venster</button>  ';
die();
}
}
?>
<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title> ontdubbelen.</title>
<meta name="language" content="nl" />
<meta name="description" content="" />
<meta name="keywords" content="" />
<style type="text/css">
table {
width:70%;
-moz-box-shadow: 10px 10px 5px #888;
-webkit-box-shadow: 10px 10px 5px #888;
box-shadow: 10px 10px 5px #888;
background-color:#efefef;
border-radius:8px;
}
. btn
{
background: #FFFFFF;
border: 1px solid #000;
font-size: 10pt;
color: #416cd2;
border:1px solid #000000;
border-radius: 12px;
padding-left:5px;
padding-right:5px;
width:100%;
}
.btn:hover
{
background: green;
border: 1px solid #000;
font-size: 10pt;
color: white;
border:1px solid #000000;
border-radius: 12px;
}
a {
text-decoration:none;
color: #0099FF;
}
a:hover {
text-decoration:none;
color: #000000;
}
button
{
width: 30%;
background-color: red;
color: white;
padding: 5px 5px;
margin: 8px 0;
border: 1px solid #000000;
border-radius: 12px;
cursor: pointer;
}
</style>
<body>
<table align="center" class="table" widtg="70%">
<tr><td>
<form method="post" enctype="multipart/form-data" name="opschonen" onkeypress="return event.keyCode != 13;">
<center>U kunt een lijst met achternamen opschonen.<br><br>
<label>Wilt u de lijst ontdubbelen?</label>
<select name="ontdubbel" >
<option value="JA" selected> JA</option>
<option value="NEE" > NEE</option>
</select>
<input type="submit" name="confirm" value="Ga door"><br/>
</form>
<br><br>
<p align="center"><button onclick="self.close()">Sluit dit venster</button>
</td></tr>
</table>
</center>
</body>
</html>
ini_set('display_errors', 1);
error_reporting(E_ALL);
?>
<?php
if ($_SERVER['REQUEST_METHOD']=="POST") {
$ontdubbelen=$_POST['ontdubbel'];
if ($ontdubbelen=="JA") {
echo '<center>Ontdubbelen is gestart, momentje...';
$lijst="Adressenlijst.txt";
$lines = file($lijst);
$result = array_unique($lines);
file_put_contents($lijst, var_export($result, true));
echo '<br><center><b>De lijst is ontdubbeld.</b><center><br>';
echo '<p align="center"><button onclick="self.close()">Sluit dit venster</button>  ';
die();
} else {
echo '<br><center>Er is iets misgegaan. Bewerking gestopt!</center><br>';
echo '<p align="center"><button onclick="self.close()">Sluit dit venster</button>  ';
die();
}
}
?>
<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title> ontdubbelen.</title>
<meta name="language" content="nl" />
<meta name="description" content="" />
<meta name="keywords" content="" />
<style type="text/css">
table {
width:70%;
-moz-box-shadow: 10px 10px 5px #888;
-webkit-box-shadow: 10px 10px 5px #888;
box-shadow: 10px 10px 5px #888;
background-color:#efefef;
border-radius:8px;
}
. btn
{
background: #FFFFFF;
border: 1px solid #000;
font-size: 10pt;
color: #416cd2;
border:1px solid #000000;
border-radius: 12px;
padding-left:5px;
padding-right:5px;
width:100%;
}
.btn:hover
{
background: green;
border: 1px solid #000;
font-size: 10pt;
color: white;
border:1px solid #000000;
border-radius: 12px;
}
a {
text-decoration:none;
color: #0099FF;
}
a:hover {
text-decoration:none;
color: #000000;
}
button
{
width: 30%;
background-color: red;
color: white;
padding: 5px 5px;
margin: 8px 0;
border: 1px solid #000000;
border-radius: 12px;
cursor: pointer;
}
</style>
<body>
<table align="center" class="table" widtg="70%">
<tr><td>
<form method="post" enctype="multipart/form-data" name="opschonen" onkeypress="return event.keyCode != 13;">
<center>U kunt een lijst met achternamen opschonen.<br><br>
<label>Wilt u de lijst ontdubbelen?</label>
<select name="ontdubbel" >
<option value="JA" selected> JA</option>
<option value="NEE" > NEE</option>
</select>
<input type="submit" name="confirm" value="Ga door"><br/>
</form>
<br><br>
<p align="center"><button onclick="self.close()">Sluit dit venster</button>
</td></tr>
</table>
</center>
</body>
</html>
Toevoeging op 24/12/2017 13:43:51:
Bovengenoemde actie werkt. Alles wordt ontdubbeld, echter er staat n meer in de Adressenlijst dan ik wil. Wie weet hier raad me?
Uitvoer Adressenlijst.txt na de routine doorlopen te hebben
array (
0 => 'een
',
1 => 'twee
',
2 => 'drie
',
3 => 'vier
',
4 => 'vijf
',
15 => 'jan
',
16 => 'piet
',
17 => 'klaas
',
18 => 'Jan
',
19 => 'Piet
',
20 => 'Klaas',
)
Toevoeging op 24/12/2017 13:45:22:
Jan te Pas op 23/12/2017 22:19:08:
In een lijst, een txt-bestand, staan allemaal achternamen onder elkaar, elk op een nieuwe regel. En die lijst wil ik inlezen, ontdubbelen en weer opnieuw als txt-bestand wegschrijven. Dat probeer ik op te lossen.
Toevoeging op 24/12/2017 12:14:32:
Ik heb nu de volgende code gemaakt. Ik kan dit testen, maar wil graag horen of dit de goede richting is.
Toevoeging op 24/12/2017 13:43:51:
Bovengenoemde actie werkt. Alles wordt ontdubbeld, echter er staan meer in de Adressenlijst dan ik wil. Wie weet hier raad mee?
Uitvoer Adressenlijst.txt na de routine doorlopen te hebben
array (
0 => 'een
',
1 => 'twee
',
2 => 'drie
',
3 => 'vier
',
4 => 'vijf
',
15 => 'jan
',
16 => 'piet
',
17 => 'klaas
',
18 => 'Jan
',
19 => 'Piet
',
20 => 'Klaas',
)
Toevoeging op 24/12/2017 12:14:32:
Ik heb nu de volgende code gemaakt. Ik kan dit testen, maar wil graag horen of dit de goede richting is.
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
?>
<?php
if ($_SERVER['REQUEST_METHOD']=="POST") {
$ontdubbelen=$_POST['ontdubbel'];
if ($ontdubbelen=="JA") {
echo '<center>Ontdubbelen is gestart, momentje...';
$lijst="Adressenlijst.txt";
$lines = file($lijst);
$result = array_unique($lines);
file_put_contents($lijst, var_export($result, true));
echo '<br><center><b>De lijst is ontdubbeld.</b><center><br>';
echo '<p align="center"><button onclick="self.close()">Sluit dit venster</button>  ';
die();
} else {
echo '<br><center>Er is iets misgegaan. Bewerking gestopt!</center><br>';
echo '<p align="center"><button onclick="self.close()">Sluit dit venster</button>  ';
die();
}
}
?>
<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title> ontdubbelen.</title>
<meta name="language" content="nl" />
<meta name="description" content="" />
<meta name="keywords" content="" />
<style type="text/css">
table {
width:70%;
-moz-box-shadow: 10px 10px 5px #888;
-webkit-box-shadow: 10px 10px 5px #888;
box-shadow: 10px 10px 5px #888;
background-color:#efefef;
border-radius:8px;
}
. btn
{
background: #FFFFFF;
border: 1px solid #000;
font-size: 10pt;
color: #416cd2;
border:1px solid #000000;
border-radius: 12px;
padding-left:5px;
padding-right:5px;
width:100%;
}
.btn:hover
{
background: green;
border: 1px solid #000;
font-size: 10pt;
color: white;
border:1px solid #000000;
border-radius: 12px;
}
a {
text-decoration:none;
color: #0099FF;
}
a:hover {
text-decoration:none;
color: #000000;
}
button
{
width: 30%;
background-color: red;
color: white;
padding: 5px 5px;
margin: 8px 0;
border: 1px solid #000000;
border-radius: 12px;
cursor: pointer;
}
</style>
<body>
<table align="center" class="table" widtg="70%">
<tr><td>
<form method="post" enctype="multipart/form-data" name="opschonen" onkeypress="return event.keyCode != 13;">
<center>U kunt een lijst met achternamen opschonen.<br><br>
<label>Wilt u de lijst ontdubbelen?</label>
<select name="ontdubbel" >
<option value="JA" selected> JA</option>
<option value="NEE" > NEE</option>
</select>
<input type="submit" name="confirm" value="Ga door"><br/>
</form>
<br><br>
<p align="center"><button onclick="self.close()">Sluit dit venster</button>
</td></tr>
</table>
</center>
</body>
</html>
ini_set('display_errors', 1);
error_reporting(E_ALL);
?>
<?php
if ($_SERVER['REQUEST_METHOD']=="POST") {
$ontdubbelen=$_POST['ontdubbel'];
if ($ontdubbelen=="JA") {
echo '<center>Ontdubbelen is gestart, momentje...';
$lijst="Adressenlijst.txt";
$lines = file($lijst);
$result = array_unique($lines);
file_put_contents($lijst, var_export($result, true));
echo '<br><center><b>De lijst is ontdubbeld.</b><center><br>';
echo '<p align="center"><button onclick="self.close()">Sluit dit venster</button>  ';
die();
} else {
echo '<br><center>Er is iets misgegaan. Bewerking gestopt!</center><br>';
echo '<p align="center"><button onclick="self.close()">Sluit dit venster</button>  ';
die();
}
}
?>
<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title> ontdubbelen.</title>
<meta name="language" content="nl" />
<meta name="description" content="" />
<meta name="keywords" content="" />
<style type="text/css">
table {
width:70%;
-moz-box-shadow: 10px 10px 5px #888;
-webkit-box-shadow: 10px 10px 5px #888;
box-shadow: 10px 10px 5px #888;
background-color:#efefef;
border-radius:8px;
}
. btn
{
background: #FFFFFF;
border: 1px solid #000;
font-size: 10pt;
color: #416cd2;
border:1px solid #000000;
border-radius: 12px;
padding-left:5px;
padding-right:5px;
width:100%;
}
.btn:hover
{
background: green;
border: 1px solid #000;
font-size: 10pt;
color: white;
border:1px solid #000000;
border-radius: 12px;
}
a {
text-decoration:none;
color: #0099FF;
}
a:hover {
text-decoration:none;
color: #000000;
}
button
{
width: 30%;
background-color: red;
color: white;
padding: 5px 5px;
margin: 8px 0;
border: 1px solid #000000;
border-radius: 12px;
cursor: pointer;
}
</style>
<body>
<table align="center" class="table" widtg="70%">
<tr><td>
<form method="post" enctype="multipart/form-data" name="opschonen" onkeypress="return event.keyCode != 13;">
<center>U kunt een lijst met achternamen opschonen.<br><br>
<label>Wilt u de lijst ontdubbelen?</label>
<select name="ontdubbel" >
<option value="JA" selected> JA</option>
<option value="NEE" > NEE</option>
</select>
<input type="submit" name="confirm" value="Ga door"><br/>
</form>
<br><br>
<p align="center"><button onclick="self.close()">Sluit dit venster</button>
</td></tr>
</table>
</center>
</body>
</html>
Toevoeging op 24/12/2017 13:43:51:
Bovengenoemde actie werkt. Alles wordt ontdubbeld, echter er staan meer in de Adressenlijst dan ik wil. Wie weet hier raad mee?
Uitvoer Adressenlijst.txt na de routine doorlopen te hebben
array (
0 => 'een
',
1 => 'twee
',
2 => 'drie
',
3 => 'vier
',
4 => 'vijf
',
15 => 'jan
',
16 => 'piet
',
17 => 'klaas
',
18 => 'Jan
',
19 => 'Piet
',
20 => 'Klaas',
)
Toevoeging op 24/12/2017 14:18:00:
OPGELOST! Dank voor de hints en aanwijzingen. De code, nu ook met sorteren, naar undercast converteren en ‘nette uitvoer’.
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
?>
<?php
if ($_SERVER['REQUEST_METHOD']=="POST") {
$ontdubbelen=$_POST['ontdubbel'];
if ($ontdubbelen=="JA") {
echo '<center>Ontdubbelen is gestart, momentje...';
$lijst="Adressenlijst.txt";
$lines = file($lijst);
$result = array_map('strtolower',$lines);
$result = array_unique($result);
sort($result);
file_put_contents($lijst, $result);
echo '<br><center><b>De lijst is ontdubbeld.</b><center><br>';
echo '<p align="center"><button onclick="self.close()">Sluit dit venster</button>  ';
die();
} else {
echo '<br><center>Er is iets misgegaan. Bewerking gestopt!</center><br>';
echo '<p align="center"><button onclick="self.close()">Sluit dit venster</button>  ';
die();
}
}
?>
<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title> ontdubbelen.</title>
<meta name="language" content="nl" />
<meta name="description" content="" />
<meta name="keywords" content="" />
<style type="text/css">
table {
width:70%;
-moz-box-shadow: 10px 10px 5px #888;
-webkit-box-shadow: 10px 10px 5px #888;
box-shadow: 10px 10px 5px #888;
background-color:#efefef;
border-radius:8px;
}
. btn
{
background: #FFFFFF;
border: 1px solid #000;
font-size: 10pt;
color: #416cd2;
border:1px solid #000000;
border-radius: 12px;
padding-left:5px;
padding-right:5px;
width:100%;
}
.btn:hover
{
background: green;
border: 1px solid #000;
font-size: 10pt;
color: white;
border:1px solid #000000;
border-radius: 12px;
}
a {
text-decoration:none;
color: #0099FF;
}
a:hover {
text-decoration:none;
color: #000000;
}
button
{
width: 30%;
background-color: red;
color: white;
padding: 5px 5px;
margin: 8px 0;
border: 1px solid #000000;
border-radius: 12px;
cursor: pointer;
}
</style>
<body>
<table align="center" class="table" widtg="70%">
<tr><td>
<form method="post" enctype="multipart/form-data" name="opschonen" onkeypress="return event.keyCode != 13;">
<center>U kunt een lijst met achternamen opschonen.<br><br>
<label>Wilt u de lijst ontdubbelen?</label>
<select name="ontdubbel" >
<option value="JA" selected> JA</option>
<option value="NEE" > NEE</option>
</select>
<input type="submit" name="confirm" value="Ga door"><br/>
</form>
<br><br>
<p align="center"><button onclick="self.close()">Sluit dit venster</button>
</td></tr>
</table>
</center>
</body>
</html>
ini_set('display_errors', 1);
error_reporting(E_ALL);
?>
<?php
if ($_SERVER['REQUEST_METHOD']=="POST") {
$ontdubbelen=$_POST['ontdubbel'];
if ($ontdubbelen=="JA") {
echo '<center>Ontdubbelen is gestart, momentje...';
$lijst="Adressenlijst.txt";
$lines = file($lijst);
$result = array_map('strtolower',$lines);
$result = array_unique($result);
sort($result);
file_put_contents($lijst, $result);
echo '<br><center><b>De lijst is ontdubbeld.</b><center><br>';
echo '<p align="center"><button onclick="self.close()">Sluit dit venster</button>  ';
die();
} else {
echo '<br><center>Er is iets misgegaan. Bewerking gestopt!</center><br>';
echo '<p align="center"><button onclick="self.close()">Sluit dit venster</button>  ';
die();
}
}
?>
<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title> ontdubbelen.</title>
<meta name="language" content="nl" />
<meta name="description" content="" />
<meta name="keywords" content="" />
<style type="text/css">
table {
width:70%;
-moz-box-shadow: 10px 10px 5px #888;
-webkit-box-shadow: 10px 10px 5px #888;
box-shadow: 10px 10px 5px #888;
background-color:#efefef;
border-radius:8px;
}
. btn
{
background: #FFFFFF;
border: 1px solid #000;
font-size: 10pt;
color: #416cd2;
border:1px solid #000000;
border-radius: 12px;
padding-left:5px;
padding-right:5px;
width:100%;
}
.btn:hover
{
background: green;
border: 1px solid #000;
font-size: 10pt;
color: white;
border:1px solid #000000;
border-radius: 12px;
}
a {
text-decoration:none;
color: #0099FF;
}
a:hover {
text-decoration:none;
color: #000000;
}
button
{
width: 30%;
background-color: red;
color: white;
padding: 5px 5px;
margin: 8px 0;
border: 1px solid #000000;
border-radius: 12px;
cursor: pointer;
}
</style>
<body>
<table align="center" class="table" widtg="70%">
<tr><td>
<form method="post" enctype="multipart/form-data" name="opschonen" onkeypress="return event.keyCode != 13;">
<center>U kunt een lijst met achternamen opschonen.<br><br>
<label>Wilt u de lijst ontdubbelen?</label>
<select name="ontdubbel" >
<option value="JA" selected> JA</option>
<option value="NEE" > NEE</option>
</select>
<input type="submit" name="confirm" value="Ga door"><br/>
</form>
<br><br>
<p align="center"><button onclick="self.close()">Sluit dit venster</button>
</td></tr>
</table>
</center>
</body>
</html>
Het bovenstaande is toch zoiets als een auto bergopwaarts duwen, je kunt er ook in rijden? Om ervoor te zorgen dat data integer blijft zou je beperkingen (constraints) kunnen opleggen aan kolommen of gewoon kunnen controleren wanneer je een achternaam toevoegt of verandert of deze reeds bestaat.
Vraag ik mij ook nog steeds af waar dit voor bedoeld is? Achternamen zijn niet per definitie uniek, dus om hier nu een soort van unieke entiteit van te maken is uit (database)technisch perspectief ook een beetje raar.
@Thomas, je hebt gelijk, mijn doel is om bij elk project mysql te gebruiken, maar dit is voor een persoon, en site waar geen database achterzit of kan zitten. Dus hannesen. Een beetje oud papierhulp. Dus kies ik voor simpeler methoden. En die zijn vaak best moeilijk. Dank!
Gebruik dan op zijn minst sqlite. Dat is overal beschikbaar.