1

I'm using a downloaded PHP script to speed up development of my site, but I'm having a stack of 'deprecated' messages coming up relating to ereg. I'm going to turn off warnings before going live, but am trying to make sure I clear them all first!

I found a handy guide which is good for the simple bits, such as changing eregi_replace for preg_replace with / and /i but I'm running into problems with some of the more advanced expressions (with comments, concatenation, variables, braces and more - some are the same types, but I'm getting so confused trying and breaking it!).

Can anyone tell me where I should put the / or / /i when I update the following, or, just as welcome, tell me what do do with each type (or point me at instructions) so I can do it myself?

eregi("<!--\ startBlock\(([^)]+)\)\ -->", $content["body"], $m)

eregi($reg, $content["body"], $m)

eregi("([^\.]+)\.(.*)", $variable, $m)  ... I think (?) this is preg_replace("/([^\.]+)\.(.*)/i", $variable, $m)

eregi("{url:([^}]+)}", $txt, $m))

eregi_replace("{".$key."}", $val, $txt)

eregi_replace("{script}", $HTTP_SERVER_VARS["SCRIPT_NAME"], $txt)

eregi_replace("{ifNotSet:".$m[1].":([^}]+)}", "", $txt)

Update: I seem to have it all working (thanks for the help) except one function below, where I can't figure out the preg versions - I have tried various combinations but the resulting page never gives the same result as the eregi version (with the error line). Any suggestions?

function parse(&$content) {
while (preg_match("/<!--\ startBlock\(([^)]+)\)\ -->/i", $content["body"], $m)) {
  $name = $m[1];
  $block = array();
  $block["name"] = $name;
  $block["blocks"] = array();
  $block["used"] = 0;
  $block["values"] = array();
  $reg = "<!--\ startBlock\(".$name."\)\ -->(.*)<!--\ endBlock\(".$name."\)\ -->";
  if (!eregi("$reg", $content["body"], $m)) {
$this->error("block `".$name."' does not have startBlock() AND endBlock().");
  }
  $block["body"] = $m[1];
  $content["body"] = eregi_replace("$reg", "{".$name."}", $content["body"]);
  $content["blocks"][$name] = array();
  $this->parse(&$block);
  $content["blocks"][$name][0] = $block;
}
}

and then I'll get started on the "call-time pass-by references", which seem even trickier!

Francis Laclé
  • 384
  • 2
  • 6
  • 22
Adam
  • 49
  • 2

2 Answers2

0

I assume by the / you mean the delimiters to start and end the pattern? Think of them like the double quotes the surround the string. They need to be at the beginning and end of the pattern, and any / in the pattern need to be escaped \/, you can put modifiers at the end, like i, which states it will check the pattern case-insensitive. so /pattern/i will check for pattern (case-insensitive), and to translate say </a> to preg, it would be something like /<\/a>/ (and you can add i after if you want).

You can make the backslashes something else (say @ signs, or something), but for now, I would just use them, to keep things simple. There may be other modifications needed, but that seems to be the big one.

Jess
  • 8,628
  • 6
  • 49
  • 67
  • Does that mean that `eregi("{url:([^}]+)}", $txt, $m)) ` should become `preg_match("/{url:([^}]+)/i}", $txt, $m))`? How do I handle one like `eregi_replace("{".$key."}", $val, $txt)` where the pattern is split into a quoted part, a variable and a quoted part? Do I need to put the delimiters at the beginning of the first quote and end of the last quote, or does each bit need delimiters? – Adam Jun 27 '11 at 12:52
  • So just add `/` and add an i at the end. So the first one would turn into: `/{url:([^}]+)}/i`, and the second `"/{".$key."}/i"`. I am going to assume you are actually looking for the `{ }`, otherwise they aren't needed. – Jess Jun 27 '11 at 20:44
  • A good tester is here: http://www.switchplane.com/awesome/preg-match-regular-expression-tester – Jess Jun 27 '11 at 20:45
  • Could you instead post it as an edit in your answer, make it easier to read – Jess Jul 16 '11 at 02:10
0

The / / are just border characters, you can actually use almost any one you want, as long as they they do not occur elsewhere in the regex. /i means case-insensitive, meaning the patterns will math either upper or lower case.

The important thing to remember, is if you put \ in front of a character it will escape it, and not treat it as the special character it is.

Drazisil
  • 3,070
  • 4
  • 33
  • 53