MoFish Posted December 16, 2013 Share Posted December 16, 2013 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; Quote Link to comment https://forums.phpfreaks.com/topic/284803-reading-file-variable-help/ Share on other sites More sharing options...
MoFish Posted December 16, 2013 Author Share Posted December 16, 2013 think i might be into something with an ini file instead... Quote Link to comment https://forums.phpfreaks.com/topic/284803-reading-file-variable-help/#findComment-1462483 Share on other sites More sharing options...
MoFish Posted December 16, 2013 Author Share Posted December 16, 2013 or not Quote Link to comment https://forums.phpfreaks.com/topic/284803-reading-file-variable-help/#findComment-1462485 Share on other sites More sharing options...
Ch0cu3r Posted December 16, 2013 Share Posted December 16, 2013 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"; Quote Link to comment https://forums.phpfreaks.com/topic/284803-reading-file-variable-help/#findComment-1462489 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.