Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

How to convert a string to an array, (read from a file)

Featured Replies

Hello all,

 

I'm currently converting text from a file and then converting it to an array to display in my browser. I can get the data from the file and using a foreach loop I can convert it into an array, however, I need to convert each word into an array. At the moment I can only convert each line into an array, this line needs to be split up into a name, number and price. For example here's some sample data:

 

John@yahoo.com:7:8.35

Fred@hotmail.com:4:5.59

geoff@gmail.com:3:9.29

 

I can convert each line in an array but each element must be inserted into its own td inside a table. At the moment I can put each line into a table row but I need to split each element up to:

 

<td>John@yahoo.com</td><td>7</td><td>8.35

 

at the moment my output is:

 

<td>John@yahoo.com7:8.35</td>

 

Here's a snippet of my code:

 
function display_data($data)

        {

            $fh = fopen($data, "r") or die("Unable to open file!");

            $content = fread($fh, filesize($data));

            $assoc_array = array();

            $my_array = explode("\n", $content);

            sort($my_array);

            

            foreach($my_array as $key => $value)

            {

            

                if(is_array($data)) {

                    get_file($value);

                } elseif(is_object($data)) {

                    get_file($value);

                } else {

                

                    

                

                    $tmp = explode(" ", $value);

                   

                    $tmp = str_replace(array("#",":")," ",$tmp);

                  

                        

                    $assoc_array[$tmp[0]] = $tmp[0];

                        

                    $this->display_array($assoc_array[$tmp[0]]);

                    

                

                }

            }

            

            

            fclose($fh);

                

        }
 

the display_array is simply another function that outputs the code into a table. I know it has to be a explode, preg_split or preg_replace but my reg expression isn't great. Any help? Thanks in advance.

  • Author

It doesn't work. I've even hard coded the 'John@yahoo.com:7:8.35' assigned to the variable to make sure it wasn't data from the file but all it does is get rid of anything after the ':' so all you get is the email address everything after it is ignored.

 

Thanks anyway. I'm going to try a nested foreach and see if that works.

  • Author

Thanks. I've just inserted the explode function into my other function that outputs the table. You're right it works fine. I put the explode function into my foreach loop and it only read the first element as all I was reading was the first element, (line), but when I inserted it into my other function and typed var[0], var[1] and var[2] it works fine thanks.

 

 

 

It does work, @@BrowserBugs is right.

$str = "John@yahoo.com:7:8:35";
$new_arry = explode(":", $str);

Will give you:

$str = "John@yahoo.com:7:8:35";
$new_arry[0] = John@yahoo.com;
$new_arry[1] = 7;
$new_arry[2] = 8;
$new_arry[3] = 35;

 

Sorry missed to mention that bit :s

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.