7

socallink.txt:

"Facebook","Twitter","Twitter","google-plus","youtube","pinterest","instagram"

PHP:

$file = file_get_contents('./Temp/socallink.txt', true);
$a1 = array($file);
print_r($a1);

Result :

Array
(
    [0] => "Facebook","Twitter","Twitter","google-plus","youtube","pinterest","instagram"
)

Needed:

$a1['0']=facebook;
$a1['1']=Twitter;

3 Answers 3

7

This solves your problem :

$file = '"Facebook","Twitter","Twitter","googleplus","youtube","pinterest","instagram"'; // This is your file

First remove all the ".

$file = str_replace('"', '', $file);

Then explode at every ,

$array = explode(',',$file);

var_dump($array) gives :

array(7) {
  [0]=>
  string(8) "Facebook"
  [1]=>
  string(7) "Twitter"
  [2]=>
  string(7) "Twitter"
  [3]=>
  string(11) "google-plus"
  [4]=>
  string(7) "youtube"
  [5]=>
  string(9) "pinterest"
  [6]=>
  string(9) "instagram"
}

Global code looks like :

$file = file_get_contents('./Temp/socallink.txt', true);
$file = str_replace('"', '', $file);
$a1 = explode(',',$file);

Hope this'll help

Sign up to request clarification or add additional context in comments.

1 Comment

Please mark as correct whatever answer worked to clean the feed
2

Since those are Comma Separated Values (CSV), this would probably be the simplest:

$file = file_get_contents('./Temp/socallink.txt', true);
$a1   = str_getcsv($file);

1 Comment

Is this case you better do this array_map('str_getcsv', $source_file)
-2
<?php
//fetch the file content
    $file =  file_get_contents('your file path`enter code here`');

 //create an array by breaking the string into array values at the comma ',' point
$a = explode(',',$file);

print_r($a);

//result
// Array ([0] => "Facebook"
// [1] => "Twitter"
// [2] => "Twitter"
// [3] => "google-plus"
// [4] => "youtube"
// [5] => "pinterest"
// [6] => "instagram" )

2 Comments

What about the quotes?
Please add a description for your answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.