Jump to content

I can't seem to build a pattern that is useable, please help!


jamesxg1

Recommended Posts

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.

Link to comment
Share on other sites

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.

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.