Acs Posted July 5, 2008 Share Posted July 5, 2008 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 Link to comment https://forums.phpfreaks.com/topic/113332-solved-reason-not-to-see-character-in-str_replace-and-htmlentities/ Share on other sites More sharing options...
DarkWater Posted July 5, 2008 Share Posted July 5, 2008 Well umm....if you echo it out, the browser will display it as the correct characters that the entities represent. =P Link to comment https://forums.phpfreaks.com/topic/113332-solved-reason-not-to-see-character-in-str_replace-and-htmlentities/#findComment-582282 Share on other sites More sharing options...
Acs Posted July 5, 2008 Author Share Posted July 5, 2008 Not in the source code view.... I really hope you were just trying to be funny... Link to comment https://forums.phpfreaks.com/topic/113332-solved-reason-not-to-see-character-in-str_replace-and-htmlentities/#findComment-582374 Share on other sites More sharing options...
DarkWater Posted July 5, 2008 Share Posted July 5, 2008 I wasn't. You have no idea how many questions on this forum are answered with such a simple answer. Let me try the script on my box. One minute. Link to comment https://forums.phpfreaks.com/topic/113332-solved-reason-not-to-see-character-in-str_replace-and-htmlentities/#findComment-582376 Share on other sites More sharing options...
DarkWater Posted July 5, 2008 Share Posted July 5, 2008 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. Link to comment https://forums.phpfreaks.com/topic/113332-solved-reason-not-to-see-character-in-str_replace-and-htmlentities/#findComment-582382 Share on other sites More sharing options...
Acs Posted July 5, 2008 Author Share Posted July 5, 2008 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.... Link to comment https://forums.phpfreaks.com/topic/113332-solved-reason-not-to-see-character-in-str_replace-and-htmlentities/#findComment-582411 Share on other sites More sharing options...
DarkWater Posted July 5, 2008 Share Posted July 5, 2008 I'm sorry, I missed that part. =P When you echo $buffer in that line with Debug in front, does it display what you expect it to have at that point? Link to comment https://forums.phpfreaks.com/topic/113332-solved-reason-not-to-see-character-in-str_replace-and-htmlentities/#findComment-582415 Share on other sites More sharing options...
Acs Posted July 5, 2008 Author Share Posted July 5, 2008 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. Link to comment https://forums.phpfreaks.com/topic/113332-solved-reason-not-to-see-character-in-str_replace-and-htmlentities/#findComment-582422 Share on other sites More sharing options...
Acs Posted July 5, 2008 Author Share Posted July 5, 2008 I just upgraded and got the same results.... Please anyone! Link to comment https://forums.phpfreaks.com/topic/113332-solved-reason-not-to-see-character-in-str_replace-and-htmlentities/#findComment-582430 Share on other sites More sharing options...
Acs Posted July 5, 2008 Author Share Posted July 5, 2008 Ok ok I found the problem! It was the encoding of the file, it was not UTF-8 and so the characters were not being "seen" Thanks for the help anyway Link to comment https://forums.phpfreaks.com/topic/113332-solved-reason-not-to-see-character-in-str_replace-and-htmlentities/#findComment-582467 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.