0

i have a string and i want to convert them into an array using the character '§' :

<?php
$string="element1§element2§element3";
$array=explode("&sect;",$string); //tried also &#167; and §,double and single quotes
print_r($array);
?>

The output is always this :

Array ( [0] =>element1§element2§element3)

How can i let php recognize the '§' symbol in order to split the string into an array?

EDIT : I get the string from a file using file() function and iterating the array through a foreach. If i use my own example it works, but with the exernal file do not.

The rowurlencode() of my string is the follow:

element1%A7element2%A7element3

4 Answers4

2

Works perfectly for me

ini_set('default.charset', 'UTF-8');
header('Content-type: text/html;charset=UTF8');

$string="element1§element2§element3";
$array=explode("§",$string);

print_r($array); //Array ( [0] => element1 [1] => element2 [2] => element3 ) 
Yang
  • 8,580
  • 8
  • 33
  • 58
  • if i set ini_set() and header() it don't get anymore "§" but replace it with "�" I get the string from a file using file() function and iterating the array through a foreach. If i use my own example it works, but with the exernal file do not – Anthony Stark Pirrone Apr 03 '13 at 13:15
  • 1
    I had exactly the same issue as yours. So if you want to load data from external file, you need to ensure that file is in `UTF-8 without BOM`. For example, *Notepad++* does it. The problem is encoding.. – Yang Apr 03 '13 at 13:19
1

rawurlencode your string to see the actual code of this character.

then use this code as a delimiter

$array=explode("\xA7",$string);
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
0

If you literally used:

<?php
  $string="element1§element2§element3";
  $array=explode("§",$string);
  print_r($array);
?>

This will work. My guess is that in your case $string comes from a different source, and is not in the same character encoding as your PHP file.

Evert
  • 93,428
  • 18
  • 118
  • 189
0

I find the answer, thanks to Your Common Sense. Simply, i change my explode() function like this :

$array=explode("%A7",rawurlencode($string));

Where "%A7" is the rawurlencode of "§".