Jump to content

[SOLVED] PHP, TXT File Read


jkewlo

Recommended Posts

Well, I got it to work

 

<?PHP

$Data=file_get_contents('idnum2itemdesctable.txt');

$DataArray=explode("\n#",str_replace('\r\n','\n',$Data));
array_pop($DataArray); // Go back one.

$List=ProcessList($DataArray);

/* Process the list! */
function ProcessList($Data)
{
    $List=array();
    if(!is_array($Data))
        return array();
    foreach($Data as $DataItem)
    {
        $FirstPound=strpos($DataItem,'#');
        $Num=intval(substr($DataItem,0,$FirstPound));
        if(is_numeric($Num))
            $List[$Num]=substr($DataItem,$FirstPound+1);

    }
    return $List;
}
?>

 

:) thanks so that problem is solved, So how will I go about doing a str_replace to replace ^777777 with <font color=blue> ???

Ok so I have read the manual and have played around I understand that you have to put it into array() and all is working but It will not replace ^777777 etc.. with <font color=red> etc....

 

This is the code I have to get the Text File information, as you can see I have "ATTEMPTED" to do a str_replace on it :( with little luck

with $Search and $Replace, then I had echoed

 $List = str_replace($Search, $Replace, $List[$row->id]) 

thinking it would work, Still with little luck and have searched google for good examples on str_replace to replace and have tried several other way's as well.

 

I dont really need the <font color> to be there as long as I can get rid of the ^777777 it will be fine.

<?PHP

$Data=file_get_contents('idnum2itemdesctable.txt');

$DataArray=explode("\n#",str_replace('\r\n','\n',$Data));
array_pop($DataArray); // Go back one.

$List=ProcessList($DataArray);

/* Process the list! */
function ProcessList($Data)
{
    $Search = array("^7777777", "^000000", "^000088", "^ffffff");
    $Replace = array("<font color=blue", "<font color=red>", "<font color=purple>", "<font color=white>");
    $List = array();
    if(!is_array($Data))
        return array();
    foreach($Data as $DataItem)
    {
        $FirstPound=strpos($DataItem,'#');
        $Num=intval(substr($DataItem,0,$FirstPound));
        if(is_numeric($Num))
            $List[$Num]=substr($DataItem,$FirstPound+1);

    }
    return $List;
}
?>

Now if I wanted to remove more then one say ^000088 ^ffffff ^000000 I would make it a array like

$Remove = array('^777777', '^000088', '^000000', '^ffffff');

then in the code I would do.

 

if(is_numeric($Num))
            $List[$Num]=str_replace($Remove,'',substr($DataItem,$FirstPound+1));

 

??

Ok, Well I guess I was putting the array in the wrong area, but anyways Problem Solved, I know I have been annoying, and normally I don't ask this many questions but I would Like to thank everyone who have posted here to help me solve my problems >.<

 

A beer Round on me ><

Ok, Well I guess I was putting the array in the wrong area, but anyways Problem Solved, I know I have been annoying, and normally I don't ask this many questions but I would Like to thank everyone who have posted here to help me solve my problems >.

 

A beer Round on me >

 

It's not that, it's the fact that you post something and ask if it will work.  Instead you should try it out and tell us what the errors are, what happens, what's supposed to happen, etc...

 

A beer Round on me >

 

Ooo, Chimay for me.  :P

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.