0

As i switch to PHP7 I have problem (Uncaught Error: Call to undefined function ereg())

My question is how to change from preg_match, because when i change preg_match("[0-9]{1,2}",$head) I get output ........

My code below:

    $prevodi = explode('@',$word['prevod']);
    foreach ($zborovi as $zbor)
    {
        $atr = "";
        echo '<div class="words">';
        $tmpprev = $prevodi[$cnt];
        $pred = preg_split("[.]",trim($tmpprev));
        $len = strlen($tmpprev);
        $cut = 0;
        $lng = count($pred);
        if ($lng > 1)
        {
            $cnt1 = 0;
            while ($cnt1 < $lng-1)
            {
                $head = trim($pred[$cnt1]);
                $cut = $cut + strlen($pred[$cnt1]) + 1;
                $cnt1 = $cnt1 + 1;
                if (preg_match("/\d{1,2}/", $head)) continue;
                if (strpos($head,'(') === false || strpos($head,'(е)') !== false) $atr = $atr.$head.'. ';
            }
        }
        echo '<span class="zbor_1">'.$zbor.'<span class="atribute">&nbsp;&nbsp;'.$atr.'</span></span><br />';
Mirko Mukaetov
  • 37
  • 1
  • 1
  • 8
  • Since you switched to PHP 7, it's obvious that `ereg` is deprecated since PHP 5.3.0. `ereg` had no regex delimiters. `preg_match` requires delimiters either `/` or `#` so that would be like `preg_match("/[0-9]{1,2}/", $head)` or `preg_match("#[0-9]{1,2}#", $head)`. – Wolverine Dec 23 '16 at 22:29
  • @Perumal93 this is exactly my answer :) you can just vote it, you know... – Dekel Dec 23 '16 at 22:30
  • I was already typing it before you answered. As soon as you answered, the post was put on hold to avoid getting any answers futher. Thereby I couldn't post the answer and thought to at least provide it in comment. By the way, I have no objection voting up your answer. Done. :) – Wolverine Dec 23 '16 at 22:33
  • haha :) thanks! got my vote on one of yours too. – Dekel Dec 23 '16 at 22:33

1 Answers1

2

You can use

if (preg_match("/\d{1,2}/", $head))

Instead.

btw, note that it could also be preg_match("/[0-9]{1,2}/", $head). The only difference is that pattern in preg_match should be wrapped in start and end char (it could be any of /#+@)

Dekel
  • 60,707
  • 10
  • 101
  • 129