1

I am currently trying to validate data being pulled from an Oracle database within PHP. I need to replace any non-ASCII characters with their ASCII counterparts (For example “â” should be passed in the feed as “a”). I am trying to find a way to perform this task while looping through the array. However, I am not sure how to look at every character separately without it taking forever (it has to process about 12 columns of data for over 20,000 contact records). Here is the code I am using to loop through the array and validate other portions of the data:

    function validate_contact($array){
echo "<table border='1'>\n";
    echo "<tr><td>UNIQUE ID</td><td>LAST NAME</td><td>FIRST NAME</td><td>MIDDLE INITIAL</td><td>COUNTRY</td>
        <td>CUSTOM LABEL 1</td><td>CUSTOM VALUE 1</td><td>CUSTOM LABEL 2</td><td>CUSTOM VALUE 2</td>
        <td>CUSTOM LABEL 3</td><td>CUSTOM VALUE 3</td><td>CUSTOM LABEL 4</td><td>CUSTOM VALUE 4</td>
        <td>CUSTOM LABEL 5</td><td>CUSTOM VALUE 5</td><td>CUSTOM LABEL 6</td><td>CUSTOM VALUE 6</td>
        <td>CUSTOM LABEL 7</td><td>CUSTOM VALUE 7</td><td>CUSTOM LABEL 8</td><td>CUSTOM VALUE 8</td>
        <td>PHONE LABEL 1</td><td>PHONE 1 COUNTRY CODE</td><td>PHONE 1</td><td>PHONE EXTENSION 1</td>
        <td>CASCADE 1</td><td>PHONE LABEL 2</td><td>PHONE 2 COUNTRY CODE</td><td>PHONE 2</td><td>PHONE EXTENSION 2</td>
        <td>CASCADE 2</td><td>EMAIL LABEL 1</td><td>EMAIL 1</td><td>SMS 1</td>
        </tr>";
    while($row=oci_fetch_array($array, OCI_ASSOC+OCI_RETURN_NULLS)){
echo "<tr>\n";
        foreach ($row as $item) {
            echo "    <td>" . ($item !== null ? htmlentities($item, ENT_QUOTES) : "&nbsp;") . "</td>\n";
}
    echo "</tr>\n";
}
echo "</table>\n";
};

I would like to use an if/while/foreach statement somewhere within my current while($row=oci_fetch_array($array, OCI_ASSOC+OCI_RETURN_NULLS)) statment to successfully replace all non-ASCII characters. I tried making use of something along the lines of: $item = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $item) but I have had no luck. Any ideas?

2
  • technically this has NOTHING to do with Oracle. You're basically just asking how to "de-UTF" a string in PHP.
    – Marc B
    Jul 7, 2014 at 14:47
  • This is true, but it has to be done while parsing through the oci_fetch_array using some of its methods so I figured that I might as well mention it.
    – ITProblems
    Jul 7, 2014 at 14:52

1 Answer 1

0

You can use a library like this: https://github.com/silverstripe-labs/silverstripe-unidecode

or try using a simpler map (not as big as silverstripe):

function replaceLink( $string, $separator = '-' ) { 
    $search = array(
        'À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ', 'Ā', 'Ą', 'Ă', 'Ç', 'Ć', 'Č', 'Ĉ', 'Ċ', 'Ď', 'Đ', 'È', 'É', 'Ê', 'Ë', 'Ē', 'Ę', 'Ě', 'Ĕ', 'Ė', 'Ĝ', 'Ğ', 'Ġ',
        'Ģ', 'Ĥ', 'Ħ', 'Ì', 'Í', 'Î', 'Ï', 'Ī', 'Ĩ', 'Ĭ', 'Į', 'İ', 'IJ', 'Ĵ', 'Ķ', 'Ľ', 'Ĺ', 'Ļ', 'Ŀ', 'Ł', 'Ñ', 'Ń', 'Ň', 'Ņ', 'Ŋ', 'Ò', 'Ó', 'Ô', 'Õ',
        'Ö', 'Ø', 'Ō', 'Ő', 'Ŏ', 'Œ', 'Ŕ', 'Ř', 'Ŗ', 'Ś', 'Ş', 'Ŝ', 'Ș', 'Š', 'Ť', 'Ţ', 'Ŧ', 'Ț', 'Ù', 'Ú', 'Û', 'Ü', 'Ū', 'Ů', 'Ű', 'Ŭ', 'Ũ', 'Ų', 'Ŵ',
        'Ŷ', 'Ÿ', 'Ý', 'Ź', 'Ż', 'Ž', 'à', 'á', 'â', 'ã', 'ä', 'ā', 'ą', 'ă', 'å', 'æ', 'ç', 'ć', 'č', 'ĉ', 'ċ', 'ď', 'đ', 'è', 'é', 'ê', 'ë', 'ē', 'ę',
        'ě', 'ĕ', 'ė', 'ƒ', 'ĝ', 'ğ', 'ġ', 'ģ', 'ĥ', 'ħ', 'ì', 'í', 'î', 'ï', 'ī', 'ĩ', 'ĭ', 'į', 'ı', 'ij', 'ĵ', 'ķ', 'ĸ', 'ł', 'ľ', 'ĺ', 'ļ', 'ŀ', 'ñ',
        'ń', 'ň', 'ņ', 'ʼn', 'ŋ', 'ò', 'ó', 'ô', 'õ', 'ö', 'ø', 'ō', 'ő', 'ŏ', 'œ', 'ŕ', 'ř', 'ŗ', 'ś', 'š', 'ş', 'ť', 'ţ', 'ù', 'ú', 'û', 'ü', 'ū', 'ů',
        'ű', 'ŭ', 'ũ', 'ų', 'ŵ', 'ÿ', 'ý', 'ŷ', 'ż', 'ź', 'ž', 'ß', 'ſ', 'Α', 'Ά', 'Ἀ', 'Ἁ', 'Ἂ', 'Ἃ', 'Ἄ', 'Ἅ', 'Ἆ', 'Ἇ', 'ᾈ', 'ᾉ', 'ᾊ', 'ᾋ', 'ᾌ',
        'ᾍ', 'ᾎ', 'ᾏ', 'Ᾰ', 'Ᾱ', 'Ὰ', 'Ά', 'ᾼ', 'Β', 'Γ', 'Δ', 'Ε', 'Έ', 'Ἐ', 'Ἑ', 'Ἒ', 'Ἓ', 'Ἔ', 'Ἕ', 'Έ', 'Ὲ', 'Ζ', 'Η', 'Ή', 'Ἠ', 'Ἡ', 'Ἢ',
        'Ἣ', 'Ἤ', 'Ἥ', 'Ἦ', 'Ἧ', 'ᾘ', 'ᾙ', 'ᾚ', 'ᾛ', 'ᾜ', 'ᾝ', 'ᾞ', 'ᾟ', 'Ὴ', 'Ή', 'ῌ', 'Θ', 'Ι', 'Ί', 'Ϊ', 'Ἰ', 'Ἱ', 'Ἲ', 'Ἳ', 'Ἴ', 'Ἵ', 'Ἶ',
        'Ἷ', 'Ῐ', 'Ῑ', 'Ὶ', 'Ί', 'Κ', 'Λ', 'Μ', 'Ν', 'Ξ', 'Ο', 'Ό', 'Ὀ', 'Ὁ', 'Ὂ', 'Ὃ', 'Ὄ', 'Ὅ', 'Ὸ', 'Ό', 'Π', 'Ρ', 'Ῥ', 'Σ', 'Τ', 'Υ', 'Ύ', 'Ϋ',
        'Ὑ', 'Ὓ', 'Ὕ', 'Ὗ', 'Ῠ', 'Ῡ', 'Ὺ', 'Ύ', 'Φ', 'Χ', 'Ψ', 'Ω', 'Ώ', 'Ὠ', 'Ὡ', 'Ὢ', 'Ὣ', 'Ὤ', 'Ὥ', 'Ὦ', 'Ὧ', 'ᾨ', 'ᾩ', 'ᾪ', 'ᾫ', 'ᾬ', 'ᾭ',
        'ᾮ', 'ᾯ', 'Ὼ', 'Ώ', 'ῼ', 'α', 'ά', 'ἀ', 'ἁ', 'ἂ', 'ἃ', 'ἄ', 'ἅ', 'ἆ', 'ἇ', 'ᾀ', 'ᾁ', 'ᾂ', 'ᾃ', 'ᾄ', 'ᾅ', 'ᾆ', 'ᾇ', 'ὰ', 'ά', 'ᾰ', 'ᾱ', 'ᾲ',
        'ᾳ', 'ᾴ', 'ᾶ', 'ᾷ', 'β', 'γ', 'δ', 'ε', 'έ', 'ἐ', 'ἑ', 'ἒ', 'ἓ', 'ἔ', 'ἕ', 'ὲ', 'έ', 'ζ', 'η', 'ή', 'ἠ', 'ἡ', 'ἢ', 'ἣ', 'ἤ', 'ἥ', 'ἦ', 'ἧ', 'ᾐ',
        'ᾑ', 'ᾒ', 'ᾓ', 'ᾔ', 'ᾕ', 'ᾖ', 'ᾗ', 'ὴ', 'ή', 'ῂ', 'ῃ', 'ῄ', 'ῆ', 'ῇ', 'θ', 'ι', 'ί', 'ϊ', 'ΐ', 'ἰ', 'ἱ', 'ἲ', 'ἳ', 'ἴ', 'ἵ', 'ἶ', 'ἷ', 'ὶ', 'ί', 'ῐ',
        'ῑ', 'ῒ', 'ΐ', 'ῖ', 'ῗ', 'κ', 'λ', 'μ', 'ν', 'ξ', 'ο', 'ό', 'ὀ', 'ὁ', 'ὂ', 'ὃ', 'ὄ', 'ὅ', 'ὸ', 'ό', 'π', 'ρ', 'ῤ', 'ῥ', 'σ', 'ς', 'τ', 'υ', 'ύ',
        'ϋ', 'ΰ', 'ὐ', 'ὑ', 'ὒ', 'ὓ', 'ὔ', 'ὕ', 'ὖ', 'ὗ', 'ὺ', 'ύ', 'ῠ', 'ῡ', 'ῢ', 'ΰ', 'ῦ', 'ῧ', 'φ', 'χ', 'ψ', 'ω', 'ώ', 'ὠ', 'ὡ', 'ὢ', 'ὣ', 'ὤ',
        'ὥ', 'ὦ', 'ὧ', 'ᾠ', 'ᾡ', 'ᾢ', 'ᾣ', 'ᾤ', 'ᾥ', 'ᾦ', 'ᾧ', 'ὼ', 'ώ', 'ῲ', 'ῳ', 'ῴ', 'ῶ', 'ῷ', '¨', '΅', '᾿', '῾', '῍', '῝', '῎', '῞', '῏', '῟',
        '῀', '῁', '΄', '΅', '`', '῭', 'ͺ', '᾽', 'А', 'Б', 'В', 'Г', 'Д', 'Е', 'Ё', 'Ж', 'З', 'И', 'Й', 'К', 'Л', 'М', 'Н', 'О', 'П', 'Р', 'С', 'Т', 'У', 'Ф',
        'Х', 'Ц', 'Ч', 'Ш', 'Щ', 'Ы', 'Э', 'Ю', 'Я', 'а', 'б', 'в', 'г', 'д', 'е', 'ё', 'ж', 'з', 'и', 'й', 'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т',
        'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ы', 'э', 'ю', 'я', 'Ъ', 'ъ', 'Ь', 'ь', 'ð', 'Ð', 'þ', 'Þ',
        '\'',
    );


    $replace = array(
        'a', 'a', 'a', 'a', 'ae', 'a', 'a', 'a', 'a', 'a', 'c', 'c', 'c', 'c', 'c', 'd', 'd', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'g', 'g',
        'g', 'g', 'h', 'h', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'ij', 'j', 'k', 'k', 'k', 'k', 'k', 'l', 'n', 'n', 'n', 'n', 'n', 'o', 'o',
        'o', 'o', 'oe', 'o', 'o', 'o', 'o', 'oe', 'r', 'r', 'r', 's', 's', 's', 's', 's', 't', 't', 't', 't', 'u', 'u', 'u', 'ue', 'u', 'u', 'u', 'u',
        'u', 'u', 'w', 'y', 'y', 'y', 'z', 'z', 'z', 'a', 'a', 'a', 'a', 'ae', 'a', 'a', 'a', 'a', 'ae', 'c', 'c', 'c', 'c', 'c', 'd', 'd', 'e', 'e',
        'e', 'e', 'e', 'e', 'e', 'e', 'e', 'f', 'g', 'g', 'g', 'g', 'h', 'h', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'ij', 'j', 'k', 'k', 'l',
        'l', 'l', 'l', 'l', 'n', 'n', 'n', 'n', 'n', 'n', 'o', 'o', 'o', 'o', 'oe', 'o', 'o', 'o', 'o', 'oe', 'r', 'r', 'r', 's', 's', 's', 't', 't',
        'u', 'u', 'u', 'ue', 'u', 'u', 'u', 'u', 'u', 'u', 'w', 'y', 'y', 'y', 'z', 'z', 'z', 'ss', 'ss', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a',
        'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'b', 'g', 'd', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'z', 'i',
        'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'th', 'i', 'i', 'i', 'i', 'i', 'i', 'i',
        'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'k', 'l', 'm', 'n', 'ks', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'p', 'r', 'r', 's', 't',
        'y', 'y', 'y', 'y', 'y', 'y', 'y', 'y', 'y', 'y', 'y', 'f', 'x', 'ps', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o',
        'o', 'o', 'o', 'o', 'o', 'o', 'o', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a',
        'a', 'a', 'a', 'a', 'a', 'b', 'g', 'd', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'z', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i',
        'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'th', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i',
        'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'k', 'l', 'm', 'n', 'ks', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'p', 'r', 'r', 'r', 's',
        's', 't', 'y', 'y', 'y', 'y', 'y', 'y', 'y', 'y', 'y', 'y', 'y', 'y', 'y', 'y', 'y', 'y', 'y', 'y', 'y', 'y', 'f', 'x', 'ps', 'o', 'o', 'o',
        'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', '', '', '', '', '', '', '', '', '',
        '', '', '', '', '', '', '', '', '', 'a', 'b', 'v', 'g', 'd', 'e', 'e', 'zh', 'z', 'i', 'i', 'k', 'l', 'm', 'n', 'o', 'p', 'r', 's', 't', 'u', 'f',
        'kh', 'ts', 'ch', 'sh', 'shch', 'y', 'e', 'yu', 'ya', 'a', 'b', 'v', 'g', 'd', 'e', 'e', 'zh', 'z', 'i', 'i', 'k', 'l', 'm', 'n', 'o', 'p',
        'r', 's', 't', 'u', 'f', 'kh', 'ts', 'ch', 'sh', 'shch', 'y', 'e', 'yu', 'ya', '', '', '', '', 'd', 'd', 'th', 'th',
        '',
    );

    $url = str_replace( $search, $replace, $string );

    $url = strtolower( trim( preg_replace( "/([^a-zA-Z0-9]+)/", $separator, $url ), $separator ) );

    return $url;
}

Source: https://gist.github.com/raveren/6061530

3
  • This looks like it may be useful, but where would I put this code? Would I need to make another while statement within the oci_fetch_array loop? How can I make it search for every string within every column of data?
    – ITProblems
    Jul 7, 2014 at 14:55
  • Also, can I use this type of function to replace all instances of '&' with '&amp;' or other similar entities?
    – ITProblems
    Jul 7, 2014 at 15:03
  • Thank you very much for this response. I made a few adjustments to the function and was able to make it work to fit my needs. I ended up having to call the function with each column separately. Here is an example of my working code for those with a similar question: while($row=oci_fetch_array($array, OCI_ASSOC+OCI_RETURN_NULLS)){ if(isset($row["LAST NAME"])){ $row["LAST NAME"]=replaceASCII($row["LAST NAME"]); } if(isset($row["FIRST NAME"])){ $row["FIRST NAME"]=replaceASCII($row["FIRST NAME"]); }}
    – ITProblems
    Jul 7, 2014 at 18:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.