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

Link to comment
Share on other sites

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
}
?>

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.