1

I am new to php. I have a text file which contains text something like this

<??blah blahh blah
   blah blah blah
   .......
??>

<??blah blahh blah
   blah blah blah
   .......
??>

<??blah blahh blah
   blah blah blah
   ...... .
??>

It means My main data is in between <?? and ??> I want to make an array which will contains all main data in array (removing these <?? & ??> charachter). So that I can insert the data items in MySql table. I don't know how to make an array from file lile this.

Thanks for help!!!

1
  • @nickb As I said i am very new to php. I have never used it. But today need to make some webservices for my application.So first i need nysql database and this main data in table. I just know some basics. Commented Jul 6, 2012 at 17:34

4 Answers 4

4

Hopefully this will be of assistance.

First off, you'll need to look into the file library associated with PHP.

Reference: http://www.php.net/manual/en/ref.filesystem.php

Using fopen and fread, you can open up the file in question and parse it from there.

<?php
// get contents of a file into a string
$filename = "something.txt";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
?>

Next, we'll use some simple string manipulation to get your important information. Using split, we can cut up your file contents into the good stuff.

Reference: http://php.net/manual/en/function.split.php

<?php
// sanitize content headers
$contents = split("<\?\?", $contents);
foreach($contents as $content) {
   // remove content footers
   str_replace("??>", "", $content);
}
?>

Lastly, we'll go through all the elements in the array we've just created using split and insert them into our database.

Reference: http://php.net/manual/en/book.mysql.php

<?php
// sanitize content headers
$contents = split("<\?\?", $contents);
foreach($contents as $content) {
   if (empty($content)) {
       continue;
   }
   // remove content footers
   str_replace("??>", "", $content);

   // insert into database
   mysql_query("INSERT INTO `something` VALUES ('" . $content . "')");
}
?>

Overall, the final code should look something like this:

<?php
// get contents of a file into a string
$filename = "something.txt";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);

// sanitize content headers
$contents = split("<\?\?", $contents);
foreach($contents as $content) {
   if (empty($content)) {
       continue;
   }
   // remove content footers
   str_replace("??>", "", $content);

   // insert into database
   mysql_query("INSERT INTO `something` VALUES ('" . $content . "')");
}
?>

Good luck!

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

1 Comment

Thanks for your help. I am getting an error Deprecated: Function split() is deprecated
3

You should be able to do this with explode and a little bit of creativity, like so:

$str = file_get_contents( 'yourfile.txt');
$array = explode( '<??', $str);
array_shift( $array); // first element is empty
array_walk( $array, function( &$el) { $el = str_replace( '??>', '', $el); });
var_dump( $array);

Now, your array looks something like:

array(3) {
  [0]=>
  string(52) "blah blahh blah
   blah blah blah
   .......


"
  [1]=>
  string(52) "blah blahh blah
   blah blah blah
   .......


"
  [2]=>
  string(49) "blah blahh blah
   blah blah blah
   ...... .
"
}

Comments

2
<?php    
preg_match_all("/(?:<\?\?)(.+?)(?:\?\?>)/sm",file_get_contents("test.txt"),$result);
print_r($result[1]);
?>

Comments

1

try:

<?php

$filestring = file_get_contents('YOUR_FILE_TO_PARSE');
$pattern = '/<\?\?[\w\s.]*\?\?>/';
preg_match($pattern, $filestring, $matches);

?>

where $matches will be your array

1 Comment

This is a much more efficient way of handling this than explode or split

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.