Jump to content

[SOLVED] about quote string from file(txt)


kamikraze

Recommended Posts

hi guys.. im new here.. if u can give me link bout related topic i really appreciate it..

well my problem i assume a bit simple.. but i dont know how to do it..

the main problem is how to quote out and put into a variable from a set of string line from .txt format file.. using php code.. and definitely not relate with sql..

 

example text file:

Name: John Davis

DOB: 26 May 2009

Occupation: Student

Matric No.: AC010203

 

Name: John Davis2

DOB: 26 May 2010

Occupation: Student

Matric No.: AC010204

 

Name: John Davis3

DOB: 26 May 2011

Occupation: Student

Matric No.: AC010205

 

....(data may be more)

 

so what i dont know is how to grab the matric number (AC******) and put it in array or variable..

i really appreciate for any help that lead me to the solution..

 

Link to comment
Share on other sites

If you can read the text file into PHP already then I'd would use RegEx to get the matric no.

 

Something like (regex not tested)

 

preg_match_all("~(Matric No.(\s*)(AC[0-9]{6})~", $text, $matches);

 

If your text file is big then it may be better to read it line by line to save memory.

 

more info: http://th.php.net/manual/en/function.preg-match-all.php

Link to comment
Share on other sites

thanks everisk..

the link really help me.. but there is something i dont understand in making the match limitation..

i dont understand how it could be like that.. is there anywhere i can learn this? i dont know what it called.. hard to search.. hmm..

 

*the task changed.. i think it become more easier..

-from txt file contain data :

 

John Davis, 26 May 2009, Student,AC010203

John Davis2, 26 May 2010, Student, AC010204

John Davis3, 26 May 2011, Student, AC010205

 

-need to copy the data into other txt file

-but need to insert into array 1st before pass the value..

-the other txt file should be like:

 

Name: John Davis

DOB: 26 May 2009

Occupation: Student

Matric No.: AC010203

 

Name: John Davis2

DOB: 26 May 2010

Occupation: Student

Matric No.: AC010204

 

Name: John Davis3

DOB: 26 May 2011

Occupation: Student

Matric No.: AC010205

 

any1 can give me the int preg_match_all ( string $pattern , string $subject , array &$matches [, int $flags [, int $offset ]] ) for this problem.. i'll try find other source to learn but to solve this problem i dont think i can make it in time.. need u guy's help.. thanks..

Link to comment
Share on other sites

It's called 'Regular Expression' or RegEx and i think you could modify it a bit to get it to work with both files

 

preg_match_all("~(AC[0-9]{6})~", $text, $matches);

 

This will match everything that starts with AC and follow by 6 numbers. Trying print_r($matches) to see the result.

Link to comment
Share on other sites

If you need to rearrange all the data, you could use file() to read through every line, and store each part using explode():

 

<?php
$lines = file('filename.txt');
$data = array();
foreach ($lines as $line) {
if (!empty(trim($line))) {
	$parts = explode(',', $line);
	$data[] = array(
		'name' => trim($parts[0]),
		'dob' => trim($parts[1]),
		'occupation' => trim($parts[2]),
		'matric' => trim($parts[3])
	);
}
}
//have a look at the structured data
echo '<pre>' . print_r($data, true) . '</pre>';
?>

You should then be able to write your new text file by looping through $data

Link to comment
Share on other sites

RegEx.. i currently reading it here http://th.php.net/manual/en/reference.pcre.pattern.syntax.php.. but still dont get it all.. many expression need to understand.. pain2..

hmm.. but to get data from string "John Davis, 26 May 2009, Student,AC010203" can using that preg_match_all("~(AC[0-9]{6})~", $text, $matches);?

 

*thebadbad

thanks for the code.. its really help me out..

i understand by loop the code, it will move to another line..

so to call the data in the array like this ->$data[n][name]? or $data[n][m]? m&n is integer..

Link to comment
Share on other sites

Yes, you can access individual elements of the array with e.g. $data[n]['name']. Here's how to loop through the array, and re-structure the data into a string (to be written to the new file):

 

<?php
$str = '';
foreach ($data as $array) {
$str .= "Name: {$array['name']}\nDOB: {$array['dob']}\nOccupation: {$array['occupation']}\nMatric No.: {$array['matric']}\n\n";
}
//write string to new file
file_put_contents('newfilename.txt', $str);
?>

Link to comment
Share on other sites

"Fatal error: Can't use function return value in write context in C:\wamp\www\test2\test4.php on line 13" came out after i try to test the code..

foreach ($lines as $line)

    line 13----->if (!empty(trim($line))) {

$parts = explode(',', $line);

$data[] = array(

'name' => trim($parts[0]),

'dob' => trim($parts[1]),

'occupation' => trim($parts[2]),

'matric' => trim($parts[3])

);

* is that because i dont declare trim or what? what is the use of trim? hmm..

 

Link to comment
Share on other sites

No, trim() just strips any whitespace and new lines (and some other similar chars) from the beginning and end of the input string.

 

The error indicates a fault in your code that writes the contents to the new file. Can I see that part?

Link to comment
Share on other sites

Ok, the input format you have defined (as below):

 

John Davis, 26 May 2009, Student,AC010203

John Davis2, 26 May 2010, Student, AC010204

John Davis3, 26 May 2011, Student, AC010205

 

Can be read EASILY using fgetcsv(), which puts it all into an array.

 

<?php
$handle = fopen("filename.txt", "r");
while (($row = fgetcsv($handle, 1000, ",")) !== FALSE) {
    $data[] = $row;
}
print_r($data);
fclose($handle);
?>

Link to comment
Share on other sites

"Can I see that part?"- what u mean? the error start at line 13 which mean at this code

[pre]<?php

$lines = file('filename.txt');

$data = array();

foreach ($lines as $line) {

if (!empty(trim($line))) { //<---line 13

$parts = explode(',', $line);

$data[] = array(

'name' => trim($parts[0]),

'dob' => trim($parts[1]),

'occupation' => trim($parts[2]),

'matric' => trim($parts[3])

);

}

}

//have a look at the structured data

echo '<pre>' . print_r($data, true) . '</pre>';

?>[/pre]

 

fgetcsv? 1st time see it.. sry im really new in php.. hmm.. what is the function of that code?

*i'll try ur code soon.. thanks..

Link to comment
Share on other sites

Oh, ran the answer through Google, and found out what it means. It's because empty() can't check what's returned by a function (in this case trim()) - only a variable. So to fix it, replace this:

 

if (!empty(trim($line))) {

with this:

 

$line = trim($line);
if (!empty($line)) {

Sorry for the confusion :)

Link to comment
Share on other sites

*aschk

ur code work.. thanks..

 

*thebadbad

yeah.. it work when i put the changed code.. thanks..

 

now i want to loop it to get the data..

i try to do like this..

do{
echo $data[$x]['name'].'<br \>';
echo $data[$x]['name'].'<br \>';
echo $data[$x]['name'].'<br \>';
echo $data[$x]['name'].'<br \>'.'<br \>';
$x++;
}while($x==true)

i can't find how to count the string line available..

just find how to count substr only.. using substr_count();

 

my problem regarding txt file already solve but for a moment if can help a little bit to recall the data back.. :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.