Align sentences of a text file to left and right in PHP - php

$result = "";
while (some condition)
{
$result = file_get_contents("filename.txt");
$result = "$result\n" . $result;
file_put_contents("filename.txt", $result);
}
echo nl2br ($result);
I want to echo the first line as left aligned and second line as right aligned and so on on a web page. Is there any way to achieve this?

First you can split the text into an array buy using implode and use CSS to align them:
$result = "";
while (some condition){
$result = file_get_contents("filename.txt");
$result = "$result\n" . $result;
file_put_contents("filename.txt", $result);
}
$lines = implode('\n',$result);
echo "<div class='lines'>";
foreach($lines as $line){
echo $line;
}
echo "</div>";
// CSS:
echo '.lines > div:nth(even){ text-align:right; }';

$aLines = readfile(<file>);
for( $i = 0; $i < count( $aLines ); $i++ ) {
$align = ($i%2)?"left":"right";
echo "<div style='text-align: {$align}'>{$aLines[$i]}</div>";
}
Or if you meant to align inside the file itself:
$aLines = readfile(<source_file>);
$iLineWidth = 100;
$aLinesNew = [];
for( $i = 0; $i < count( $aLines ); $i++ ) {
$sLine = ($i%2)?$aLines[$i]:str_pad( $aLines[$i], $iLineWidth, " ", STR_PAD_LEFT);
$sLine = trim($sLine);
array_push( $aLinesNew, $sLine );
}
file_put_contents( <destination_file>, implode( "\n", $aLinesNew ) );

Related

create div from each item in tags column

My tags column is like this:
first row: sky - earth - sea
second row: iron - silver - gold
third row: apple - fruit - food
...and so on
Want to create a div from each item, like this:
<div class='tagdown'>sky</div>
<div class='tagdown'>earth</div>
$st = $db->query("select tags from posts");
$arr = array();
$items = "";
while ($row = $st->fetch()) {
array_push($arr, explode(' - ', $row['tags']));
}
foreach($arr as $item) {
$items .= "<div class='tagdown'>" . $item . "</div>\n";
}
echo $items;
Notice: Array to string conversion...
Another Try:
for ($i = 0; $i < count($arr); ++$i) {
$items .= "<div class='tagdown'>" . $arr[$i] . "</div>\n";
}
echo $items;
Notice: Array to string conversion...
Any help?
Dont push and again traverse your array. just print out data in while loop. Try following code:
$items = "";
while ($row = $st->fetch()) {
$arr = explode(' - ', $row['tags']);
$items .= "<div class='tagdown'>".implode("</div>\n<div class='tagdown'>",$arr)."</div>\n";
}
echo $items;
Try like shown below
Example :
<?php
$items = "";
$str = "sky-earth-sea";
$arr = explode("-", $str);
$count = count($arr);
for($i = 0; $i < $count; $i++) {
$items .= "<div class='tagdown'>".$arr[$i]."</div></br>";
}
echo $items;
?>
explode() returns an array and you are pushing an array into an other array
its making 1 2D array you can check thar using print_r($arr);
use this
while ($row = $st->fetch()) {
$tag=explode('-', $row['tags'];
foreach($tag as $t){
array_push($arr,$t ));
}
}
You can also use fetch associative if using mysqli_connect
while ($row = $result->fetch_assoc()) {
array_push($arr, explode(' - ', $row['tags']));
}
foreach($arr as $a) {
foreach($a as $v){
$items .= "<div class='tagdown'>" . $v . "</div>\n";
}
}
echo $items;
-------------- OR -------------
$arr = array();
$items = "";
while ($row = $result->fetch_assoc()) {
$tag = explode(' - ', $row['tags']);
foreach($tag as $v){
$items .= "<div class='tagdown'>" . $v . "</div>\n";
}
}
echo $items;

Loop through list of URLs and save content to text file

I am trying to loop through a list of URLs and save content from a div tag to a text file.
<?php
$file = 'content.txt';
$i = 406;
for($i; $i <= 1410; $i++) {
$url = 'http://example.com/chapter/chapter-'.$i;
$content = file_get_contents($url);
$start_tag = explode( '<div class="textdiv">' , $content );
$end_tag = explode("</div>" , $start_tag[1] );
$result_text = $second_step[0];
echo $result_text;
$result = file_put_contents($file, $result_text);
}
?>
The first problem is that there are multiple occurrences of the div tag with that class and I want to get every div with that class and the current code just outputs first occurrence.
[EDIT]
Thanks to The Alpha's help for pointing me to right direction, This worked for me:
<?php
include_once('simple_html_dom.php');
$i = 399;
$file = 'content.txt';
for($i; $i < 1400; $i++){
$url = 'http://example.com/chapter/chapter-'.$i;
$html = file_get_html($url);
foreach ($html->find('div.textdiv') as $div) {
echo $div . '<br />';
$result = file_put_contents($file, $div );
}
echo '<hr><br /><h1>Chapter '. $i .'</h1><br /><hr>';
}
?>
One Issue was it takes very very long time for the script to run.

php excel reader - ignore cells with special symbols

I use the parser for converting xls to csv http://code.google.com/p/php-excel-reader/
<?php
set_time_limit(300);
require_once 'excel_reader2.php';
$data = new Spreadsheet_Excel_Reader("file.xls", false, 'UTF-8');
$f = fopen('file.csv', 'w');
for($row = 1; $row <= $data->rowcount(); $row++)
{
$out = '';
for($col = 1; $col <= $data->colcount(); $col++)
{
$val = $data->val($row,$col);
// escape " and \ characters inside the cell
$escaped = preg_replace(array('#”#u', '#\\\\#u', '#[”"]#u'), array('"', '\\\\\\\\', '\"'), $val);
if(empty($val))
$out .= ',';
else
$out .= '"' . $escaped . '",';
}
// remove last comma (,)
fwrite($f, substr($out, 0, -1));
fwrite($f, "\n");
}
fclose($f);
?>
From some strange reason it skip cells with specials symbols - like ° or ®. How it can be fixed?
utf8_decode and html_entity_decode works for me:
<?php
set_time_limit(300);
require_once 'excel_reader2.php';
$data = new Spreadsheet_Excel_Reader("file.xls", false, 'UTF-8');
$f = fopen('file.csv', 'w');
for($row = 1; $row <= $data->rowcount(); $row++)
{
$out = '';
for($col = 1; $col <= $data->colcount(); $col++)
{
$val = $data->val($row,$col);
// escape " and \ characters inside the cell
$escaped = preg_replace(array('#”#u', '#\\\\#u', '#[”"]#u'), array('"', '\\\\\\\\', '\"'), $val);
$escaped = utf8_decode($escaped);
//$escaped = html_entity_decode($escaped);
if(empty($val))
$out .= ',';
else
$out .= '"' . $escaped . '",';
}
// remove last comma (,)
fwrite($f, substr($out, 0, -1));
fwrite($f, "\n");
}
fclose($f);
?>
Output:
"1","2","3","4","5"
"a","b","c","d","e"
"6","7","°","9","10"
"q","w","e","r","t"
"®","12","13","14","15"
"z","x","c","v","b"

Why isn't this returning all of the results in this while loop?

I'm trying to write a video ripper and my while loop is only returning 1 result.
<?php
$url = 'http://www.SITE.com/categories/redhead';
$url2 = 'http://www.SITE.com/movies';
$search = file_get_contents($url);
$results = explode('"/movies', $search);
$count = count($results);
$i = 1;
while($i < 5) {
$final = $url2 .$results[$i];
$goodfinal = str_replace('">', ' ', $final);
echo $goodfinal.'<br>';
$i++;
}
?>
Perhaps use your iterator to access the correct $results entry:
$url = 'http://www.SITE.com/categories/redhead';
$url2 = 'http://www.SITE.com/movies';
$search = file_get_contents($url);
$results = explode('"/movies', $search);
$i = 0;
$length = count($results);
while($i < $length) {
$final = $url2 .$results[$i];
$goodfinal = str_replace('">', ' ', $final);
echo $goodfinal.'<br>';
$i++;
}
As an alternative you could loop over your results like this:
foreach($results as $result) {
echo str_replace('">', ' ', $url2 . $result) . '<br>';
}

About displaying posts from two or more blogs

I want to display posts of two or more blogs in my website now am using magpierss-0.72 for fetching the posts and my code is
require_once('rss_fetch.inc');
$url = 'http://rajs-creativeguys.blogspot.com/feeds/posts/default?alt=rss'
/*'http://raghuks.wordpress.com/feed/'*/;
$rss = fetch_rss($url);
foreach ($rss->items as $i => $item ) {
$title = strtoupper ($item['title']);
$url = $item['link'];
$date = substr($item['pubdate'],0,26);
//code to fetch only some text
$desc = '';
$max = 30;
$arr = explode(' ', strip_tags($item['description']));
$l = count($arr);
if($l < $max) $max = $l;
for($j=0;$j<$max;++$j) {
$desc .= $arr[$j] . ' ';
}
$desc .= '.....';
echo "<div class=\"blog\"><a target=\"_blank\" href=$url><h1>$title</h1>$desc<br/><br/>DATED : $date <br/><br/></a></div> ";
if($i == 3) break;
}
Here i can specify only one url of feeds and can fetch but now i want to display posts of two or more blogs Please give me the solution
Thanks in advance
Just use an array and throw in another foreach:
<?php
require_once('rss_fetch.inc');
$urls = array(
'http://rajs-creativeguys.blogspot.com/feeds/posts/default?alt=rss',
' more urls ... ',
);
foreach($urls as $url) {
/*'http://raghuks.wordpress.com/feed/'*/;
$rss = fetch_rss($url);
foreach ($rss->items as $i => $item ) {
$title = strtoupper ($item['title']);
$url = $item['link'];
$date = substr($item['pubdate'],0,26);
//code to fetch only some text
$desc = '';
$max = 30;
$arr = explode(' ', strip_tags($item['description']));
$l = count($arr);
if($l < $max) $max = $l;
for($j=0;$j<$max;++$j)
{
$desc .= $arr[$j] . ' ';
}
$desc .= '.....';
echo "<div class=\"blog\"><a target=\"_blank\" href=$url><h1>$title</h1>$desc<br/><br/>DATED : $date <br/><br/></a></div> ";
if($i == 3) break;
}
}

Categories