-1

Possible Duplicate:
Converting ereg expressions to preg

I am totally new to parsing a cfg file to php. Still tried to search couple of website and i found this exmaple. But it has some errors in it. I dont understand how can i solve them.
I have a .cfg file named my_config.cfg. It has the information as shown below.

# ATA/IDE/MFM/RLL support
#
CONFIG_IDE=y

#
# IDE, ATA and ATAPI Block devices
#
CONFIG_BLK_DEV_IDE=y
CONFIG_BLK_DEV_IDEDISK=y
CONFIG_BLK_DEV_IDECD=n

I am trying to parse it in php.

Code:

<?php
$config_file = "my_config.cfg";
$comment = "#";

$fp = fopen($config_file, "r");

while (!feof($fp)) {
$line = trim(fgets($fp));
if ($line && !ereg("^$comment", $line)) {
$pieces = explode("=", $line);
$option = trim($pieces[0]);
$value = trim($pieces[1]);
$config_values[$option] = $value;
}
}
fclose($fp);

if ($config_values['CONFIG_IDE'] == "y")
echo "CONFIG_IDE is set&lt;br /&gt;";
else
echo "CONFIG_IDE is not set&lt;br /&gt;";
?>

But I am getting error:

Deprecated: Function ereg() is deprecated in C:\xampp\htdocs\test.php on line 9
CONFIG_IDE is set<br />

Any Solutions???

Community
  • 1
  • 1
ashah142
  • 560
  • 1
  • 5
  • 12

2 Answers2

2

That deprecation warning indicates you should find a different function to perform what you need. More info on PHP's ereg documentation page. How about preg_match() instead?

if ($line && !preg_match("/^$comment/", $line)) {
    ...
}

But instead of using a regular expression, you might be able to get away with using substr(), like this:

if ($line && substr($line, 0, 1) != $comment) {
    ...
}

Firing up the regex matching subsystem on each iteration of your loop is expensive, performance-wise. substr() is way cheaper!

By the way, what about using parse_ini_file() here? In my tests of your code and your sample Linux kernel config snippet, it produces the same thing, but with 1 line of code instead of 12.

curtisdf
  • 4,130
  • 4
  • 33
  • 42
  • I tried `preg_match` but still some error : `Warning: preg_match() [function.preg-match]: No ending delimiter '^' found in C:\xampp\htdocs\test.php on line 9 Notice: Undefined offset: 1 in C:\xampp\htdocs\test.php on line 12` – ashah142 Jul 03 '12 at 19:37
  • Oopse, I forgot the delimiters. I fixed my answer. Should work now. Good luck! – curtisdf Jul 03 '12 at 19:38
  • Thanks `curtisdf` and `sachleen` for your help...it worked... – ashah142 Jul 03 '12 at 19:40
  • hey one more question...i was able to do this successfully because my config file was in the same folder with my php. But what if i want to use the config file remotely? Any idea? – ashah142 Jul 03 '12 at 19:47
  • You can still make it work with a remote config file. `fopen()` works if you give it a full URL. – curtisdf Jul 03 '12 at 19:48
1

You have &amp;&amp;... that should be &&

if ($line && !ereg("^$comment", $line)) {

As of PHP 5.3.0, ereg has been deprecated. You should be using preg_match instead. The syntax will be the same for you.

sachleen
  • 30,730
  • 8
  • 78
  • 73
  • i tried that but i am getting this error. `Deprecated: Function ereg() is deprecated in C:\xampp\htdocs\test.php on line 9 Deprecated: Function ereg() is deprecated in C:\xampp\htdocs\test.php on line 9 Deprecated: Function ereg() is deprecated in C:\xampp\htdocs\test.php on line 9 Deprecated: Function ereg() is deprecated in C:\xampp\htdocs\test.php on line 9 Deprecated: Function ereg() is deprecated in C:\xampp\htdocs\test.php on line 9 Deprecated: Function ereg() is deprecated in C:\xampp\htdocs\test.php on line 9 CONFIG_IDE is set
    `
    – ashah142 Jul 03 '12 at 19:30
  • Use `preg_match` instead. See updated answer. – sachleen Jul 03 '12 at 19:34
  • Ok. I'll try it with preg_match...Thanks `sachleen` – ashah142 Jul 03 '12 at 19:34