Jump to content

Reading File Variable Help


MoFish

Recommended Posts

Hi,

 

I am trying to read some hex values from a style-sheet, however am having a little bit of a problem reading the values from a file into a string due to them being different with no unique identifier.

 

Is there an easy way to read the file and put get the value "#e51400" for a variable called $red and "#0076a3" for a variable called $blue?

 

I looked into str_replace but couldn't see a simple way to do this.

 

Thanks,

 

MoFish

@red: #e51400;
@blue: #0076a3;
Link to comment
https://forums.phpfreaks.com/topic/284803-reading-file-variable-help/
Share on other sites

You could use regex.

$text = '@red: #e51400;
@blue: #0076a3;';

preg_match_all('~@(\w+:\s+#[a-f0-9]+);~', $text, $matches);

foreach($matches[1] as $match)
{
    list($var, $value) = explode(':', $match);

    $$var = trim($value);
}

echo "Red: $red<br />
Blue: $blue";

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.