dcsimg
www.webdeveloper.com
Results 1 to 7 of 7

Thread: PHP charset utf8; copy value in array

  1. #1
    Join Date
    Nov 2014
    Posts
    4

    PHP charset utf8; copy value in array

    Hi,

    So I have this code...
    The problem is that $data is correctly utf8, because i echo it and can see the correct string. However when I pass it to $name, this charset is broken.
    If i wish now to display this new array, it shows question marks on the black diamond shaped figure.

    Could you please maybe help me debug this code?
    The php file has this in the header.
    <meta charset="UTF-8">
    <meta http-equiv="Content-type" content="text/html; charset=UTF-8">



    PHP Code:

    <?php
    $data 
    = array();
    $inc 0;
    $handle = @fopen("content_realisations.php""r");
    if (
    $handle) {
        while ((
    $buffer fgets($handle4096)) !== false) {
            
    $data[$inc] = ($buffer);
            
    $inc $inc+1;
        }
        if (!
    feof($handle)) {
            echo 
    "Error: unexpected fgets() fail\n";
        }
        
    fclose($handle);
    }

    ?>



    <?php

    //for ($i=0; $i<$inc; $i++){
        
    $name get_data($data$inc);
        
    //echo utf8_encode($name[5][2]);
        
    echo $name[5][2];
    //    echo $values[1] . "<br>";
    //    echo $values[2] . "<br>";
         
    echo '<pre>'print_r($name); echo '</pre>';
    //}

    //echo $inc; $length = strlen(utf8_decode($data[22])); echo $length . "<br>"; echo $data[22][$length-3];
        
    function get_data($data$inc){
        for (
    $row=0$row<$inc$row++){
            if (
    $mode == 0){
                
    $z=0$y=0$w=0;
                
    $data2 = array();
                for(
    $i=$iutf8_decode(strlen($data[$row])) ; $i++){
                    if ((
    $data[$row][$i] == '>') and ($z 3)){
                        
    $z++;
                        
    $data_start $i;
                        
    //echo $i . "<br>";
                        
    }
                    if ((
    $data[$row][$i] == '<') and ($y 4)){
                        
    $y++;
                        
    $data_end $i;
                        
    //echo $i . "<br>";
                    
    }        
                    if (
    $data[$row][$i] == '"'){
                        
    $data2[$w] = $i;    
                        
    $w++;
                    }    
                }
                
    $file substr($data[$row], $data2[2]+1$data2[3]-$data2[2]-1);
                
    $thumb substr($data[$row], $data2[6]+1$data2[7]-$data2[6]-1);
                
    $id substr($data[$row], $data2[0]+1$data2[1]-$data2[0]-1);
                
                
    //echo $id . "<br>";echo $file . "<br>";echo $thumb . "<br>";echo '<pre>'; print_r($data2); echo '</pre>';
                
    $s $data_start+1;
                
    //echo $z . "<br>"; echo $y . "<br>"; echo $row . "<br>";
                
    if ($s $data_end and $z!=and $y!=and $id == "im")  {
                    
    //$name[$row][0] = $row; //must change the index !!!!
                    
    while($s != $data_end){
                        
    $name[$row][$s-$data_start] = $data[$row][$s];
                        echo 
    $data[$row][$s];
                        
    $s++;
                    }    
                }
            }
        
            
    $length strlen(utf8_decode($data[$row])); 
            
    $a1=$data[$row][0] . $data[$row][1] . $data[$row][2];    
            
    //echo '<pre>'; print_r($a1); echo '</pre>';
            //echo gettype($a1[0]), "\n";
            
    $is_match = (similar_text($a1"<!-") == 3) ;
            if (
    $is_match == 1){
                
    //echo "1";
                
    $mode 1;
            }else{
                if (
    similar_text($a1"-->") == or similar_text($data[$row][$length-3] . $data[$row][$length-2], "->") == 2){
                    
    $mode 0;
                }
                
    //echo "0";    
            
    }
            
    //echo $file . "<br>";
            //echo $thumb . "<br>";    
        
    }
        return 
    $name
    }


    ?>

  2. #2
    Join Date
    Nov 2014
    Posts
    4
    Here I copy the the values from one array to another

    while($s != $data_end){
    $name[$row][$s-$data_start] = $data[$row][$s];
    echo $data[$row][$s];
    $s++;
    }

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    20,262
    What's in the HTTP response headers? (I believe they take precedence over any meta tag settings.) You may want to set them via PHP's header() function.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

  4. #4
    Join Date
    Nov 2014
    Posts
    4
    I have tried both through html and php header(); both fail.
    is it possible that fgets does a bad job with utf8 extraction?

  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    20,262
    I've not ever really tried to manipulate multi-byte strings in PHP like that, so am not really sure. I do know there is a multi-byte string extension that you might want to look at.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

  6. #6
    If I were to take a wild guess, I'd say that your using "string as array indexes" is breaking the multi-byte order. You are probably copying byte-by-byte instead of character by character, which means your reversing the order is what's screwing it up as you are swapping MSB and LSB of the multi-byte characters. You might want to look at using substr instead... which would probably be WAY more efficient than brute-forcing with a loop anyways. Remember, PHP is glue; glue between the function library, sql data and markup. If you are doing anything that complex code-wise in a brute force character by character manner, you're probably doing something wrong.

    Though looking at this code I really have to ask -- just what the blazes are you even trying to do here?!? No offense but this looks needlessly and pointlessly convoluted for something that... well, looks like it should be simpler though I cannot fathom what it's even supposed to be doing.

    NOT that I'd EVER be reading in the contents of a .php file with fopen. Hell, years ago I proposed securing PHP by making it so include can't call non .php files and fopen/file_get_contents can't open .php files! Something I STILL think should be done...

    Of course if you have to error suppress fopen, you ARE doing something wrong.
    Last edited by deathshadow; 11-04-2014 at 02:15 AM.

  7. #7
    Join Date
    Nov 2014
    Posts
    4
    Quote Originally Posted by deathshadow View Post
    If I were to take a wild guess, I'd say that your using "string as array indexes" is breaking the multi-byte order. You are probably copying byte-by-byte instead of character by character, which means your reversing the order is what's screwing it up as you are swapping MSB and LSB of the multi-byte characters. You might want to look at using substr instead... which would probably be WAY more efficient than brute-forcing with a loop anyways. Remember, PHP is glue; glue between the function library, sql data and markup. If you are doing anything that complex code-wise in a brute force character by character manner, you're probably doing something wrong.

    Though looking at this code I really have to ask -- just what the blazes are you even trying to do here?!? No offense but this looks needlessly and pointlessly convoluted for something that... well, looks like it should be simpler though I cannot fathom what it's even supposed to be doing.

    NOT that I'd EVER be reading in the contents of a .php file with fopen. Hell, years ago I proposed securing PHP by making it so include can't call non .php files and fopen/file_get_contents can't open .php files! Something I STILL think should be done...

    Of course if you have to error suppress fopen, you ARE doing something wrong.
    Hi, this is exactly what was happening. I changed my function and now it works as expected.

    In terms of what I am trying to do is I want that only a php file which will be on the server, can modify other php files. I don't want to use databases because, a new let's say project that I add on the website is dependant on many other files that correspond to that project. Therefore I want to create a script in which I add the information different, corresponding to that project, upload all necessary photos, and the script manages the creation of the extra files and puts in place everything.

    Having said this, some guidance would be much appreciated, if this method looks not so appropriate.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles