How to read a file content line by line using PHP code

Suppose we have file named file.txt we have read a file content line by line using PHP , here our objective is to retrieve a file data line by line using PHP.we know we have most famous php function fopen(“File name”,”r”),it will read entire content from the files and store it in the defined variable
see example

 $f_data = fopen("file.txt","r");
 

Now our question how to read line by line using PHP
Now we know the entire data is their in $f_data,next we have to read line by line using PHP , with the help of while loop we can do that ,for line by line read we use one more function called feof($f_data) which find out the end of the file see full example

 
 $f_data = fopen("file.txt","r");
  
  while(! feof($f_data))  {
	$result = fgets($f_data);
	echo $result."
"; echo "Good"."
"; } fclose($f_data);

e.g
file.txt

Sunday is weekend day
Monday is weekstart day

Out Put

Sunday is weekend day
Good
Monday is weekstart day
Good

How to find length of array using php

we are luck enough to find the length of an array using php, we have two build in function to find the count of an array using php.Lets see in details that function

1) sizeof() function

2) count() function

First let we check out the syntax of sizeof() function

 sizeof($Array);
example

$colors=array("RED","BLUE","GREEN");
echo sizeof($colors);

Output

3


let we check out the syntax of count() function

 count($Array);
example

$colors=array("RED","BLUE","GREEN");
echo count($colors);

Output

3

how to add google news feed in your websites using PHP

Here we are going to discus how to add google news feed in your websites using PHP ,we know that google is proving XML for news feed here is the link for that

https://news.google.com/rss/search?q=KEYWORD

Here KEYWORD is search query what we are going to add as news feed in your website

This link output xml formatted news feed from that latest websites we can use same xml in your site ,we can use PHP function to convert from one suitable format

Suppose we need to display Cricket World Cup news feed in your website you have to replace KEYWORD with ‘Cricket World Cup’ see example API link below

https://news.google.com/rss/search?q=Cricket+World+Cup&hl=en-IN&gl=IN&ceid=IN:en

Next Step to get the data from the XML

we need to use simplexml_load_file for that

See below code

 


$xml=simplexml_load_file("https://news.google.com/rss/search?q=Cricket+World+Cup&hl=en-IN&gl=IN&ceid=IN:en");
$arrayG = json_decode(json_encode((array)$xml), TRUE);

TO display latest 10 item we have to use

$arrayFeed=array_slice($arrayG['channel']['item'], 0,2);
foreach($arrayFeed as $key=>$value)
{

echo "</pre>
<div class="col-md-9 nfspara">".$value['description']."</div>
"; }

see demo out from the below link
News Feed

Out Put

add google news feed in your websites using PHP

All inside pages are redirecting to http://localhost/dashboard/ in WordPress xampp localhost

When we install WordPress in localhost may face an issue,All inside pages are redirecting to http://localhost/dashboard/ in WordPress localhost

suppose we installed a working WordPress site inside demo folder in htdocs directory and changed the wp_option table siteurl and home option value to http://localhost/demo/

Here is my .htaccess files


RewriteEngine On
RewriteBase /
RewriteRule ^index\.php?$ – [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

here http://localhost/demo/contact_us it is redirecting to http://localhost/dashboard/

To fix this issue you have to change .htaccess files like this

added below code in .htaccess files
RewriteBase /demo/
RewriteRule . /demo/index.php [L]

Here is the .htaccess files


RewriteEngine On
RewriteBase /demo/
RewriteRule ^index\.php?$ – [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /demo/index.php [L]