0

I am converting an old PHP4-5 script to make it PHP 7 compatible.

I have the following line:

if ( eregi('^'.$_POST["category"],$r->id) ) $Category_List .= (' selected ');

Can I simply do this?

if ( preg_match('^'.$_POST["category"],$r->id) ) $Category_List .= (' selected ');

or do I need to do this?

if ( preg_match('"/^('.$_POST["category"],$r->id).')/"i' ) $Category_List .= (' selected ');
marcnyc
  • 585
  • 1
  • 4
  • 17
  • Do not forget the regex delimiters with preg_quote,`preg_match('/^'.preg_quote($_POST["category"], '/') . '/',$r->id)` – Wiktor Stribiżew May 08 '20 at 15:46
  • isn't my second example correct then @WiktorStribiżew? In the second example I have the '/' as regex delimiters and I have the ^ right after the first one to match the beginning of a string, and the 'i' flag after the last one to make it case insensitive... is it not correct the way I did it? – marcnyc May 08 '20 at 16:05
  • If you try you will see. – Wiktor Stribiżew May 08 '20 at 16:09
  • @WiktorStribiżew I realized I put the regex delimiter after the string instead of before, so I really meant to say this `if ( preg_match('"/^('.$_POST["category"].')/"i', $r->id )` - is this correct? – marcnyc May 08 '20 at 16:21
  • But you still need to make sure special chars are escaped, so use preg_quote. – Wiktor Stribiżew May 08 '20 at 16:42

0 Answers0