Welcome Guest, Not a member yet? Register   Sign In
Problem with CodeIgniter PHP function not changing variable
#1

[eluser]Unknown[/eluser]
Hi,

I have a CodeIgniter class (not derived from anything) that has a function. This function is aimed at replacing keywords to link in a given text. The keywords are read from the DB. The problem I am having is that no matter what the $result is always the initial value. It seems that the variable retains its original content no matter what. I tried using other cycle types. The $result is not part of the query so I am clueless as to why it doesnt update. The value of result always reverts to the original string for some reason. It should incrementally keep replacing words in the string and return it. The $limit tells the cycle that it needs to stop after that amount of replacements have been made.

I tried with an array, also inserting it into a db during iterations but always the same results. Please help as this is very annoying as I cannot find a solution.

Thanks,
Sintax

Code:
class String_Module
{
    
    function String_Module()
    {
        $this->ci =& get_instance();

        log_message('debug', 'String Module Initialized');

        // Load required library
        $this->ci->load->library('Session');
        $this->ci->load->database();

    
        // Initialize
        $this->_init();
    }

    /* Private function */

    function _init()
    {

    }

function str_replace_first($or_string, $string_to_rep, $rep_with,&$found) {
        $mylen = strlen ( $string_to_rep );
        $pos = strpos ( $or_string, $string_to_rep );
        if ($pos === FALSE) {
            $found=false;
            return $or_string;
        } else {
            $mystr = substr_replace ( $or_string, $rep_with, $pos, $mylen );
            $found = true;
            return $mystr;
        }
    }
    
    function replace_keywords($string)
    {
$sql = "SELECT * FROM keys order by length(key_name) desc";
        $q = $this->ci->db->query($sql);
        
        $result = $string;
        $cnt = 0;
$limit = 10;

foreach ($q->result_array() as $row)
        {
            $id = $row['key_id'];
            $from = $row['key_name'];
            $what = '<a href="myurl.com">'.$row['key_name'].'</a>';
        
        
            $found = false;
            
            $result = $this->str_replace_first($result,$from,$what,$found);
            
            echo "F=".$from." W=".$what." X=$found C=$cnt <br/>";
            var_dump($result); //the value of result always reverts to the original string for some reason
            if ($found){
                $cnt++;
            }
            
            if ($cnt==$limit)
                break;
        }
return $result;
}

}//class
#2

[eluser]WanWizard[/eluser]
The only way for $result to be identical after calling str_replace_first(), it if the $from is not found. When I test that function here, it replaces without problems.

What is the result of that echo statement, when you change it to
Code:
echo "F=".$from." W=".$what." X=".($found?"TRUE":"FALSE")." C=$cnt <br/>";

Is $found TRUE?




Theme © iAndrew 2016 - Forum software by © MyBB