CakeFest 2025 Madrid: The Official CakePHP Conference

Voting

: max(two, zero)?
(Example: nine)

The Note You're Voting On

aaron at aarongough dot com
16 years ago
My solution below was slightly incorrect, so here is the correct version (I posted at the end of a long day, never a good idea!)

Again, this is a quick and dirty solution to stop mb_convert_encoding from filling your string with question marks whenever it encounters an illegal character for the target encoding.

<?php
function convert_to ( $source, $target_encoding )
{
// detect the character encoding of the incoming file
$encoding = mb_detect_encoding( $source, "auto" );

// escape all of the question marks so we can remove artifacts from
// the unicode conversion process
$target = str_replace( "?", "[question_mark]", $source );

// convert the string to the target encoding
$target = mb_convert_encoding( $target, $target_encoding, $encoding);

// remove any question marks that have been introduced because of illegal characters
$target = str_replace( "?", "", $target );

// replace the token string "[question_mark]" with the symbol "?"
$target = str_replace( "[question_mark]", "?", $target );

return
$target;
}
?>

Hope this helps someone! (Admins should feel free to delete my previous, incorrect, post for clarity)
-A

<< Back to user notes page

To Top