Tags down


file_get_contents or alternative to extract the first x lines

By : Anastasis Gerakidis
Date : October 18 2020, 03:08 PM
Does that help You can read the file using dedicated method calls instead of the one-for-all file_get_contents():
code :
$fp = fopen('my/file/name', 'r');
for ($i = 0; $i < 4; $i++) {
    if (feof($fp)) {
        echo 'EOF reached';
    echo fgets($fp);

Share : facebook icon twitter icon

Alternative to file_get_contents?

By : wayang
Date : March 29 2020, 07:55 AM
Any of those help Use cURL. This function is an alternative to file_get_contents.
code :
function url_get_contents ($Url) {
    if (!function_exists('curl_init')){ 
        die('CURL is not installed!');
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $Url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $output = curl_exec($ch);
    return $output;

Alternative way to extract lines from text (python-regex)

By : guicotsch
Date : March 29 2020, 07:55 AM
I wish this helpful for you I am looking for a way to extract lines from a pretty big database in python. I need to retain only those containing one of my keywords. I thought I could use regex to solve the problem and I've put together the code below. Unfortunately, it gives me some errors (perhaps also due to the fact that my keywords, which are written in separate lines in the file listtosearch.txt, are indeed large in number, nearly 500). , Here is my code:
code :
import re
data = open('database.txt', 'r')
fileout = open("fileout.txt","w+")

with open('listtosearch.txt', 'r') as f:
    keywords = [line.strip() for line in f]

# one big pattern can take time to match, so you have a list of them
patterns = [re.compile(keyword) for keyword in keywords]

for line in data:

    for pattern in patterns:
        if not pattern.search(line):
"Name jhon" (1995)
"Name foo" (2000)
"Name fake" (3000)
"Name george" (2000)
"Name george" (2500)
"Name (george)"
"Name george" (2000)

Alternative of file_get_contents function

By : paul.pillai
Date : March 29 2020, 07:55 AM
I wish this help you In case you’re using PHP to retrieve data from a certain server you probably came across the problem that it may work for you but a client complained about lots of errors. It’s pretty likely that you’ve relied on the fact that allow_url_fopen is set to true. This way you can put pretty much anything – local path or a URL – into function calls like include or maybe simplexml_load_file.
If you’d like to get around this problem you can advice your client to make the necessary changes in his php.ini file. Most of the time this isn’t an option because the hosting company decided to disable this feature for security reasons. Since almost everybody got cURL installed we can use this to retrieve data from another web server.
code :
class XMLWrapper {

  public function loadXML($url) {
    if (ini_get('allow_url_fopen') == true) {
      return $this->load_fopen($url);
    } else if (function_exists('curl_init')) {
      return $this->load_curl($url);
    } else {
      // Enable 'allow_url_fopen' or install cURL.
      throw new Exception("Can't load data.");

  private function load_fopen($url) {
    return simplexml_load_file($url);

  private function load_curl($url) {
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $result = curl_exec($curl);
    return simplexml_load_string($result);


//For Json

class JsonWrapper {

  public function loadJSON($url) {
    if (ini_get('allow_url_fopen') == true) {
      return $this->load_fopen($url);
    } else if (function_exists('curl_init')) {
      return $this->load_curl($url);
    } else {
      // Enable 'allow_url_fopen' or install cURL.
      throw new Exception("Can't load data.");

  private function load_fopen($url) {
    $raw = file_get_contents($url);
    $data = json_decode($raw);
    return $data;

  private function load_curl($url) {
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $result = curl_exec($curl);
    $data = json_decode($result);
    return $data;


Is there an alternative to file_get_contents?

By : Rehan Abbas
Date : March 29 2020, 07:55 AM
With these it helps There are many alternatives to file_get_contents I've posted a couple of alternatives below.

Is there an alternative for file_get_contents?

By : shahzaib
Date : March 29 2020, 07:55 AM
like below fixes the issue I tried this but getting only one character from file comments.txt , You can simplify it like ..
Related Posts Related Posts :
  • How to change variable in "Many to Many" relationship?
  • Laravel session expires immediately (Laravel 5.5)
  • Remove elements containing specific char from array in PHP
  • Error in Active Directory: "ldap_search: "Search: Other (e.g., implementation specific) error"
  • PHP first index gets treated as string
  • Get count of regex matches in array
  • Passing variables to another page with url - PHP
  • &raquo; in a codeigniter form function
  • Select part of the URL inside an iframe tag
  • migrating a yii2 website from windows to linux, broken links
  • Laravel update upload using AJAX
  • Laravel: Controller method receives empty argument when called
  • How to send data in jquery with POST method?
  • mysql select distinct row, that appears at least (n) times
  • php-mysql query ouptut inside html drop-down menu
  • PHP Get a specific property from an array of objects with different array count
  • How to select users based on the role that they have in Laravel 5?
  • My MySQL prepared statement won't work
  • Why are all of my images getting cut off when I pull the binary out of a SQL Server DB?
  • Laravel Custom Pivot Model is missing fields
  • Image in php index.php file
  • Codeigniter email only works with my email
  • $.post() possible race condition; issues with redirect
  • Displaying PHP variable in html
  • How to use strpos to match most of a word?
  • get value by key of php object array
  • AXIOS returning an empty array on ajax request
  • Laravel Model Relation Where Many-to-Many Exists
  • Create several DIV containers using for loop in PHP
  • The file "prueba.jpeg" was not uploaded due to an unknown error
  • how to search in array to find all sub arrays that contain a certain value
  • post request with nested json
  • PHP posting a variable in a variable using mysql
  • How to login to an ASP.NET webpage using PHP cURL?
  • website works on local development, but errors on hosting platform
  • VSCode PHP Debug extension path translation, local to server
  • Type From right to left html / php
  • Laravel MVC (PHP), Request error messages
  • laravel 5.5 : php artisan tinker : ErrorException : Directory not empty
  • Default deposit on a percentage of the total cart amount with exceptions
  • How to get an email with log details when errors occur with Slim and Monolog
  • CI_Session has a deprecated constructor error after upgrading to codeigniter 3
  • Bypass "trying to get property of non-object" error in Laravel
  • Get ID value that references foreign key
  • How to display images in navigable modal?
  • How to pass a boolean argument to a function to act as a switch?
  • Messages counter is showing total number of messages instead of count for logged user in Laravel
  • Regex for Lastname within letter range
  • Silverstripe 3.5 - How to query for specific columns and return an array
  • How to access MS SQL Server Data Web Using PHP
  • How to extract a filename using regex from an absolute path
  • I've got a JSON file that I'm trying to extract values from with PHP
  • PHP convert string into date time
  • multiple row insert into database with mysql
  • Vinkla instagram in Laravel 5.5 and Show some Error
  • Php script returning array array?
  • php security to prevent malware insertion
  • Collect duplicate data in array using php
  • JSON File upload with multiple input file
  • Removing url / domain from string with parentheses
  • shadow
    Privacy Policy - Terms - Contact Us © soohba.com