Jump to content

[SOLVED] Reason not to see character in str_replace and htmlentities


Acs

Recommended Posts

Hey!

 

I have this simple method in my string helper class to replace some letters with their html entities but the problem I having is that for some reason the letters are not getting replaced.

 

Here is the method:

 

public static function htmlentities_specific($text) {      
      $letters = array(
        'ã','á','à','â','å','ä',
        'Ã','Á','À','Â','Å','Ä',					
        'é','è','ê','ë',
        'É','È','Ê','Ë',
        'ì','í','î','ï',
        'Ì','Í','Î','Ï',
        'ó','ò','ô','õ','ö','ø',
        'Ó','Ò','Ô','Õ','Ö','Ø',
        'ú','ù','û','ü',
        'Ú','Ù','Û','Ü',
        'ý','ÿ','Ý','ç','Ç',
        'æ','Æ','ñ','Ñ',
      );
      $entities = array(
	        'ã','á','à','â','å','ä',
	        'Ã','Á','À','Â','Å','Ä',
	        'é','è','ê','ë',
	        'É','È','Ê','Ë',
	        'ì','í','î','ï',
	        'Ì','Í','Î','Ï',
	        'ó','ò','ô','õ','ö','ø',
	        'Ó','Ò','Ô','Õ','Ö','Ø',
	        'ú','ù','û','ü',
	        'Ú','Ù','Û','Ü',
	        'ý','ÿ','Ý','ç','Ç',
	        'æ','Æ','ñ','Ñ'
      );      
  
        
      $text = str_replace(
			        $letters,
			        $entities,
			        $text,
			        $count
	        );				
echo "Debug - " , $count;
echo htmlentities($text); //This is to test! The htmlentities function also fails to replace the ç and ã
      return $text;

}

 

Here is the string I am using to test this:

 

<td>

      Nome: Paulo Teixeira<br>

  Idade: 35<br>

        Formação: Monitor de Cardio-Fitness, <br>

          Musculação, Recuperação Muscular<br>

 

  Preparação de Atletas de Alta competição.<br>

 

   

    </td>

 

---

 

As you can see there are characters that should be replaced like ç and ã, the problem is that they are not getting replaced.

I think it's the way I am passing this, because for some reason this fails in my framework, but if I just place the text inside a var and then call the method all works.

 

This method is being called inside my view class, here is the render method:

 

public function render($return = false,$parseWestern = true) {				
	if ($this->cache_status() && $this->cache_exists()) {
		$buffer = $this->cache_get_view();	
	}
	else {
		if (!$this->view_path)
			throw new acs_exception(VIEW_ERROR_NOVIEWSET);

		$this->cache_destroy(); //clear cache of this view

		ob_start();
		if (count($this->data) > 0) 			
			extract($this->data);		

	  	require($this->view_path);
	  	
	  	$buffer = ob_get_contents();
	  	ob_end_clean();
	  	
	  	if ($this->cache_status())  //Create a cachefile
	  		$this->cache_create_view($buffer);		  	
	}

  	$this->seen = true; //The view has been seen (or it's going to be )

	if ($parseWestern) {	  
	  
	  echo "Debug" , $buffer; //<-- I placed this to see what I was getting and it's the test string above
	  $buffer = helper_strings::htmlentities_specific($buffer);
	  echo $buffer; //I then placed this here to confirm that the function isn't doing anything
	}
	  		
	if ($return) 
		return $buffer; 

	echo $buffer;		
}


 

I then call this in my controller as this

 


//The false in the loadview is not to cache the view and the false in the render is so that it will echo instead of returning the code
$this->v->loadview("maximaforma/staff",false)->render(false);

 

 

The correct output would be something like:

 

Formação: Monitor de Cardio-Fitness, <br>

 

          Musculação, Recuperação Muscular<br>

  Preparação de Atletas de Alta competição.<br>

 

Where all the ã and ç have been replaced with their corresponding htmlentities values, but what I get is just the string as it was entered.

 

If anyone can please spot any errors or something please let me know

 

Thanks,

Acs

 

 

 

 

 

The function works fine.  I just used:

<?php
function htmlentities_specific($text) {      
      $letters = array(
        'ã','á','à','â','å','ä',
        'Ã','Á','À','Â','Å','Ä',					
        'é','è','ê','ë',
        'É','È','Ê','Ë',
        'ì','í','î','ï',
        'Ì','Í','Î','Ï',
        'ó','ò','ô','õ','ö','ø',
        'Ó','Ò','Ô','Õ','Ö','Ø',
        'ú','ù','û','ü',
        'Ú','Ù','Û','Ü',
        'ý','ÿ','Ý','ç','Ç',
        'æ','Æ','ñ','Ñ',
      );
      $entities = array(
	        'ã','á','à','â','å','ä',
	        'Ã','Á','À','Â','Å','Ä',
	        'é','è','ê','ë',
	        'É','È','Ê','Ë',
	        'ì','í','î','ï',
	        'Ì','Í','Î','Ï',
	        'ó','ò','ô','õ','ö','ø',
	        'Ó','Ò','Ô','Õ','Ö','Ø',
	        'ú','ù','û','ü',
	        'Ú','Ù','Û','Ü',
	        'ý','ÿ','Ý','ç','Ç',
	        'æ','Æ','ñ','Ñ'
      );      
  
        
      $text = str_replace(
			        $letters,
			        $entities,
			        $text
	        );				
       return $text;

}
$string = " <td>
         Nome: Paulo Teixeira

        Idade: 35

        Formação: Monitor de Cardio-Fitness,

                        Musculação, Recuperação Muscular


                    Preparação de Atletas de Alta competição.


   
    </td>";
echo htmlentities_specific($string);
?>

 

And got:

<td>
         Nome: Paulo Teixeira

        Idade: 35

        Formação: Monitor de Cardio-Fitness,

                        Musculação, Recuperação Muscular


                    Preparação de Atletas de Alta competição.


   
    </td>

 

Therefore it must be something in the render() method.

 

I did say that in my post!

That it didn't work when it was invoked in the render method. What I don't understand is why.

 

I also tried putting htmlentities and that didn't work when it was called from the render method. It changed the < and > to it's entities but not the ç and the ã

It's like it doesn't see those characters, and I really want to understand why, because you can see in the render method that I just call the helper_strings::htmlentities_specific with the $buffer var as it's parameter, and that's the var that holds the code.

 

Why is this not working... it works separate but not via the render.... strange....

The echo "Debug" , $buffer just echos the normal text.

The echo after should echo the changed text, but no, it outputs the exact same text.

 

I have seen my php version and it's 5.2.5 I am going to upgrade to 5.2.6. This might be a problem with the language because as far as I can see the code is simple and it's just not outputting what it's suppose to.

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.