View Single Post
ישן 21-08-09, 10:56   # 3
Shay Ben Moshe
משתמש - היכל התהילה
 
מיני פרופיל
תאריך הצטרפות: Oct 2007
הודעות: 1,397

Shay Ben Moshe לא מחובר  

Sorry for the english, I just installed Linux.
The solution is to use a foreach loop.
If this is 1d array than you should use a function like this:
PHP קוד:
function ArrSpecialChars($array) {
    foreach(
$array as $key=>$val) {
        
$array[$key] = htmlspecialchars($val);
    }

But, if it is a 2d or more array you should use something recursive like this:
PHP קוד:
function ArrSpecialChars($array) {
    foreach(
$array as $key=>$val) {
        if(
is_array($val)) {
            
$array[$key] = ArrSpecialChars($val);
        }
        
$array[$key] = htmlspecialchars($val);
    }
    return 
$array;

Of cures this one will work for both cases.

Edit:
You can use this function as a master function, for strings to arrays..
PHP קוד:
function SpecialChars($text) {
    if(
is_array($text)) {
        foreach(
$text as $key=>$val) {
            if(
is_array($val)) {
                
$text[$key] = SpecialChars($val);
            }
            
$text[$key] = htmlspecialchars($val);
        }
        return 
$text;
    }
    elseif(
is_string($text)) {
        
$text htmlspecialchars($text);
        return 
$text;
    }
    else {
        return 
false;
    }

__________________
שי בן משה - בונה אתרים
חותך אתרים, ומתכנת צד לקוח וצד שרת.

Last edited by Shay Ben Moshe; 21-08-09 at 10:59..
  Reply With Quote