Jump to content

get multiple values out of variable?


aximbigfan

Recommended Posts

Hi.

 

I'm coding a PHP log viewer, and one thing is that before it only supported 2 logs at a time. Now, I want to make it support an unlimited amount of logs.

 

This thign is, that I need to find a way to store multiple values in one variable, because when a user saves the loginfo to the ini file, it needs to come out liek this:

$log1 = "FILE:elog.txt: NAME:Error Log:"

 

how could I do that? And then set those values in an array?

 

Chris

Link to comment
https://forums.phpfreaks.com/topic/96291-get-multiple-values-out-of-variable/
Share on other sites

With a delimiter such as '|'.  So in the log you would see:

FILE:elog.txt|NAME:Error Log|

FILE:elog2.txt|NAME:Error Log2|

etc...

 

Now to get those into an array (assume all of the log data is in $log)

$array=explode("|", $log);

 

That will make something like

Array(

    0

        0 - FILE:elog.txt

        1 - NAME:Error Log

    1

        0 - FILE:elog2.txt

        1 - NAME:Error Log2

)

 

Hope it made sense.

Alternatively you can use regex.

 

<?php
$subject = 'FILE:elog.txt|NAME:Error Log
FILE:elog2.txt|NAME:Error Log2|';

if (preg_match('/FILE:([^|])++|NAME:([^|\\r\\n])++/', $subject, $matches) ) {
print_r($matches);
} else {
// no matches found
}
?>

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.