Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Insert data into text File
Old 05-27-2005, 02:12 PM Insert data into text File
Experienced Talker

Posts: 32
Trades: 0
Hello hi

I have a text file that i am trying to insert data into the text file but still maintain the structure of the text file.

function file_get_contents($file) {
$file = file($file);
return !$file ? false : implode('', $file);
}

function implode_assoc($inner_glue, $outer_glue, $array) {
$output = array();
foreach( $array as $key => $item )
$output[] = $key . $inner_glue . $item;

return implode($outer_glue, $output);
}


$hostdata = file_get_contents("http.conf");
$datasplit= split("\n", $hostdata);

//echo $datasplit;
foreach ($datasplit as $rec)
{
if (preg_match("/listen/i", $rec))
{

}
}

This is the code i have so far what happens here is that the data from the file is read into an array then i use the split function to split the data in the array at the end of every line.
Then i go through each line in the array using the foreach command looking for a certain thing in a line. Let say i am looking for the word listen.

What i am triyng to do is after the world listen is found. i go to the next line and enter a data lets say listen 1 2 3 4 5. Then continues to search the array for
listen 2 - when listen 2 is found it will stop again go to a new line and enter data again.
After this data entered in to the arry i need to write the data back into the text file. Please i am asking you to help me with please i need to get this done by atlest friday. That would be great if you dont mind thank you. Very much. If you have any futher question please contact me on aim rcubes85
rcubes85 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 05-27-2005, 10:56 PM Can anyone else please
Experienced Talker

Posts: 32
Trades: 0
Can anyone please help me or give me a suggestion on how to approach the problem listen above please
rcubes85 is offline
Reply With Quote
View Public Profile
 
Old 05-27-2005, 11:36 PM
CYH-webmaster's Avatar
Junior Talker

Posts: 4
Trades: 0
Doesn't seem too tough.. I didn't test this but I think it may work for you - a new function up top, and your conditional structure's been filled in:

PHP Code:
<?
/** function AddArrayItem($targetArray as array name string, $insertPos as array key integer, $insertItems as comma seperated string)
Add $insertItems to the $targetArray at the $insertPos position
*/
        
function addArrayItem($targetArray$insertPos$insertItems) {
   
$arrItems = array("'".$insertItems."'");
   
$firstpart array_slice($targetArray0$insertPos);
   
$secondpart array_slice($targetArray$insertPos);
   return 
array_merge($firstpart$arrItems$secondpart);
}

function 
file_get_contents($file) {
    
$file file($file);
    return !
$file false implode(''$file);
}

function 
implode_assoc($inner_glue$outer_glue$array) {
    
$output = array();
    foreach( 
$array as $key => $item )
    
$output[] = $key $inner_glue $item;

    return 
implode($outer_glue$output);
}


$hostdata file_get_contents("http.conf");
$datasplitsplit("\n"$hostdata);

foreach (
$datasplit as $key=>$rec)
{
    if (
preg_match("/listen/i"$rec))
    {
        
/* Add a value to the $datasplit array 
        of lines at the current position + 1 */
   
$lineloc $key+1;
   
$newline 'Whatever you want';
   
$datasplit addArrayItem($datasplit$lineloc$newline);   
   }
}
?>
__________________

Please login or register to view this content. Registration is FREE

2500MB/40GB transfer per month, 100 emails, PHP,
MySQL, cPanel, crontab, and more for only $99/yr!
Why pay more to pay more often?
CYH-webmaster is offline
Reply With Quote
View Public Profile Visit CYH-webmaster's homepage!
 
Old 05-28-2005, 01:13 AM
Experienced Talker

Posts: 32
Trades: 0
Thanks alot man it works like a charm
rcubes85 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Insert data into text File
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.41651 seconds with 12 queries