/    Sign up×
Community /Pin to ProfileBookmark

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″>

[code=php]

<?php
$data = array();
$inc = 0;
$handle = @fopen(“content_realisations.php”, “r”);
if ($handle) {
while (($buffer = fgets($handle, 4096)) !== false) {
$data[$inc] = ($buffer);
$inc = $inc+1;
}
if (!feof($handle)) {
echo “Error: unexpected fgets() failn”;
}
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=0 ; $i< utf8_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!=0 and $y!=0 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, “–>”) == 3 or similar_text($data[$row][$length-3] . $data[$row][$length-2], “->”) == 2){
$mode = 0;
}
//echo “0”;
}
//echo $file . “<br>”;
//echo $thumb . “<br>”;
}
return $name;
}

?>

[/code]

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@vladspyauthorNov 03.2014 — 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++;
}
Copy linkTweet thisAlerts:
@NogDogNov 03.2014 — 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.
Copy linkTweet thisAlerts:
@vladspyauthorNov 03.2014 — I have tried both through html and php header(); both fail.

is it possible that fgets does a bad job with utf8 extraction?
Copy linkTweet thisAlerts:
@NogDogNov 03.2014 — 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.
Copy linkTweet thisAlerts:
@deathshadowNov 04.2014 — 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. [i]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...[/i]

Of course if you have to error suppress fopen, you ARE doing something wrong.
Copy linkTweet thisAlerts:
@vladspyauthorNov 04.2014 — 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. [i]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...[/i]

Of course if you have to error suppress fopen, you ARE doing something wrong.[/QUOTE]


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.
×

Success!

Help @vladspy spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...