php - deprecated function ereg_replace() is deprecated in php

Redwan picture Redwan · Aug 20, 2013 · Viewed 8.1k times · Source

I Just Bought a PHP Script That Was Working Properly Till I Test It ON my Web Server. When I ran THe Script It looks Great But When I click the Sub-Category of my Website I gives me This Error "Deprecated: Function ereg_replace() is deprecated in /home/*/public_html/includes/functions.php on line 61"

My Script is Look LIke This:

<?php function generate_link($pagename,$c='',$k='',$q='',$p='',$ktext=''){

if (USE_SEO_URLS==true){

    switch ($pagename){

        case 'category.php':
            $result ='c' . $c . '.html';
            break;
        case 'questions.php':
            $result ='q-c' . $c . '-k' . $k . '-p' . $p . '-' . str_replace(" ","-",$ktext) . '.html';
            break;
        case 'answer.php':
            $result ='a' . $q . '-c' . $c . '-k' . $k . '.html';
            break;
    }

}
else {
    switch ($pagename){
        case 'category.php':
            $result ='category.php?c=' . $c;
            break;
        case 'questions.php':
            $result ='questions.php?c=' . $c . '&k=' . $k . '&p=' . $p . '&ktext=' . str_replace(" ","-",$ktext) ;
            break;
        case 'answer.php':
            $result ='answer.php?k=' . $k . '&c=' . $c . '&id=' . $q ;
            break;
    }
}

return $result;  } function db_prepare_input($string) {
if (is_string($string)) {
  return trim(sanitize_string(stripslashes($string)));
} elseif (is_array($string)) {
  reset($string);
  while (list($key, $value) = each($string)) {
    $string[$key] = db_prepare_input($value);
  }
  return $string;
} else {
  return $string;
} } function sanitize_string($string) {
$string = ereg_replace(' +', ' ', trim($string));

return preg_replace("/[<>]/", '_', $string);}?>

Sorry , My Code Is also Not Proper Formatted. I face Great Problem When I post this Question Stackoverflow. Any Help Is appreciated. The Error Is Occur in Line 61. I am New in PHP. I check, There is ereg and preg Both Functions are present. please help me.. Thanks

Answer

Bora picture Bora · Aug 20, 2013

ereg_replace deprecated. Use preg_replace like this:

$string = preg_replace('/ \+/', ' ', trim($string));

That is important / \+/ pattern. Space and plus(+)