Jump to content

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


jamesxg1

Recommended Posts

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.

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.

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

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.

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.