jamesxg1 Posted December 19, 2010 Share Posted December 19, 2010 Hiya peeps! I can't seem to build a preg_match pattern that works. This is what I need to do. "[" . $errorID . "]='HERE IS WHAT I NEED TO DISPLAY';" Does anyone have any ideas on how I would build this regex string? Many thanks, James. Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted December 19, 2010 Share Posted December 19, 2010 what have you tried? can you post your code? and an example of the exact string you are parsing... Quote Link to comment Share on other sites More sharing options...
jamesxg1 Posted December 19, 2010 Author Share Posted December 19, 2010 Example; [1]='MYSQL Error Detected.'; [2]='Class Error Detected.'; [3]='Read File Error Detected.'; Current (preg_match not working) code; <?php class error { private $errorID; public function __construct() { } public function displayError($errorID) { if(isset($_SESSION['preferredLang']) && !empty($_SESSION['preferredLang']) && strlen($_SESSION['preferredLang']) == 2) { $lang = strtolower($_SESSION['preferredLang']); } else { $lang = 'en'; } $langSource = file_get_contents($lang . '.lang', FILE_USE_INCLUDE_PATH); if($langSource) { if(preg_match("#[" . $errorID . "]='(*?)';#", $langSource, $match)) { return print_r($match); } else { return 'Error.'; } } else { return 'Error.'; } } } ?> Tried; preg_match("/[" . $errorID . "]='(*?)';/", $langSource, $match) preg_match("/(?P<errorID>\w+)='(?P<name>\w+)';/", $langSource, $match) preg_match('/[/' . $errorID . '/]/', $langSource, $match) I did try some more but I can't find go back in the editor that far. Many thanks, James. Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted December 19, 2010 Share Posted December 19, 2010 <?php $errors = array("[1]='MYSQL Error Detected.';","[2]='Class Error Detected.';","[3]='Read File Error Detected.';"); $pattern = "/\[([0-9]*)\]='(.*)\.';/"; foreach ($errors AS $an_error) { preg_match($pattern, $an_error, $matches); print_r($matches); } ?> Quote Link to comment Share on other sites More sharing options...
jamesxg1 Posted December 19, 2010 Author Share Posted December 19, 2010 Hiya BlueSkyIS, Thank you for a prompt reply and your example code; the only issue I have is with the code you supplied I would have to manually input all the data into an array, and then the code would be able to run. What I originally need to do is grab the source from a file that is already made and then string search it with preg_match and display its content. EG; if $errorID was set to 1 I would need to get; [1]='This is the error I need to display.'; I'm stumped as to what regex I would use. Many thanks, James. Quote Link to comment Share on other sites More sharing options...
jamesxg1 Posted December 19, 2010 Author Share Posted December 19, 2010 B.U.M.P Quote Link to comment 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.