I am trying to export all domains located in domains.txt file that match exactly with the links from the all-urls.txt file. This is my script:
<?php
if (isset($_POST['submit'])) {
$badwords = file('domains.txt', FILE_SKIP_EMPTY_LINES);
$domains = file('all-urls.txt', FILE_SKIP_EMPTY_LINES);
$newarr = array();
echo '<table>';
foreach($badwords as $k=>$v) {
foreach($domains as $k1=>$v1) {
if(strpos($v1,$v)!==false) {
array_push($newarr,$v1);
$links = array_shift($newarr);
echo '<tr><td>';
echo $links;
echo '</td></tr>';
}
}
}
echo '</table>';
}
?>
<body>
<form action="" name="submit">
<p><label>Ready to submit</label></p>
<p><input name="submit" type="submit" value ="Go"></p>
</form>
</body>
Instead of $badwords = file('domains.txt', FILE_SKIP_EMPTY_LINES);
I had
$badwords = array('domain1.com', 'domain2.com');
and instead of
$domains = file('all-urls.txt', FILE_SKIP_EMPTY_LINES);
I had
$domains = array('domain1.com/url/subfoler.html', 'domain1.com/url/subfoler.html','domain2.com/sublink/otherthings.php');
And right now i am trying to replace the array with file because it's way easier for me to load them like this because i have a large number of domains and url.
The problem is that the script doesn't do anything in this form. Where am i mistaking
How do you know that you actually have read the file?
$badwords = file('domains.txt', FILE_SKIP_EMPTY_LINES);
if ( $badwords === false )
die ("error on file " . 'domains.txt');
$domains = file('all-urls.txt', FILE_SKIP_EMPTY_LINES);
if ( $domains === false )
die ("error on file " . 'all-urls.txt');
...
additionally, the expressions
array_push($newarr,$v1);
$links = array_shift($newarr);
are quite the same as
$newarr = array();
$links = $v1;
and use some cpu cycles with no effect at all ...
The default method to post a form, is GET, so if you don't specify a method, GET will be used and you will never enter the section where you compare the files as:
isset($_POST['submit'])
is false
You can add a method to the form to solve that:
<form action="" name="submit" method="post">
Found my answer:
<?php
$domains = file("domains.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$urls = file("all-urls.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$newarr = array();
$f = fopen("result.txt", w);
echo "<table>";
foreach($domains as $k=>$v) {
foreach($urls as $k1=>$v1) {
if(strpos($v1,$v)!==false) {
array_push($newarr,$v1);
$links = array_shift($newarr);
echo "<tr><td>";
echo $links;
echo "</td></tr>";
fwrite($f, "$links\r\n");
}
}
}
echo "</table>";
?>
I needed to add the FILE_IGNORE_NEW_LINES, because the urls are placed in new lines.
Related
<?php
$userName = array();
$tutorial = array();
$myFile = "students.txt";
$fh = fopen($myFile,'r');
while( !feof($myFile) ){
$userName[] = array(fgets($fh));//Save first line content
$tutorial[] = array(fgets($fh));//Save second line content
}
fclose($myFile);
echo "$userName";
echo "$tutorial";
?>
and my students.txt content
dasdsa
A
asdasd
D
How to read that and store into different array and print them out
your code should work as expected. I assume you're bit confused with echo "$userName"; output as it displays Array word. try var_dump($userName) instead
Do exactly as you've done, but change
$userName[] = array(fgets($fh));//Save first line content
$tutorial[] = array(fgets($fh));//Save second line content
to
$userName[] = fgets($fh);//Save first line content
$tutorial[] = fgets($fh);//Save second line content
(no need to keep the subitems in their own seperate array)
and print them out by either using print_r, or iterate through them:
for ($i = 0; $i < count($userName); $i++) {
echo $userName[$i] . " - " . $tutorial[$i];
}
$text = file_get_contents('students.txt');
$text = explode("\n",$text);
$output = array();
foreach($text as $line)
{
$output[] = $line;
}
Use function file() in PHP
file — Reads entire file into an array
$array_lines = file('students.txt');
$count = count($array_lines);
$first_arr = array();
$sec_arr = array();
foreach ($array_lines as $i => $line){
if($i%2) $first_arr[] = $line;
else $sec_arr[] = $line;
}
print_r($first_arr);
print_r($sec_arr);
With file() function every line is read as element in array. You can check it with:
print_r($first_arr);
I have following example PHP code and its working great! but i just want one more addition so it print line number too.
<?php
$path = shell_exec('cat data.txt');
$path = chop($path,"\n");
$lines = explode("\n",$path);
echo "<h2>List of Studies</h2>";
foreach($lines as $line) {
echo "<h3><p>$line</p></h3>";
}
?>
Output:
ABC
XYZ
123
I want following addition and add counter in it.
1. ABC
2. XYZ
3. 123
You can assign the index to variable too, not just the value:
foreach($lines as $index => $line) {
printf('<h3><p>%d. %s</p></h3>', $index + 1, $line);
}
If you don't want to use the index php, you can use the tag ol
Reff: http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_lists
$i=1;
foreach($lines as $line) {
echo "<h3><p>$i. $line</p></h3>";
$i++;
}
<?php
$path = shell_exec('cat data.txt');
$path = chop($path,"\n");
$lines = explode("\n",$path);
echo "<h2>List of Studies</h2>";
$c = 0;
foreach($lines as $line) {
$c++
echo "<h3><p>".$c.". ".$line."</p></h3>";
}
?>
http://www.php.net/manual/en/control-structures.foreach.php
foreach($lines as $i=>$line) {
echo "<h3>$i. <p>$line</p></h3>";
}
here is my code. What I am trying to achieve is get text like this
Hola Hi
Pollo Chicken
Queso Cheese
and so on, and be able to make an array out of it such that
array[0][1] is Hi.
here is my code, the error is on line 13
<?php
if(isset($_POST['submit'])){
$message = $_POST['text'];
$words2 = explode("\r\n", $message);
$words = explode("\t", $words2[0]);
$numberoflines = count($words2);
echo $numberoflines;
for($i=0; $i<$numberoflines; $i++){
$words[$i] = $line;
$arrayline = explode("\t", $line);
$cow = array(
for($u=0; $u<2; $u++){
array($arrayline[$u])
}
);
}
}
?>
<html>
<form method = "POST" method ="changetext.php">
<textarea name="text">
</textarea>
<input type="submit" value = "Flip!" name="submit">
</form>
</html>
Maybe thats what you wanted to achieve ?!?
for($i=0; $i<$numberoflines; $i++){
$arraycols= explode("\t", $words[$i]);
foreach($arraycols as $col){
$list[$i][] = $col;
}
}
so Array $list is $list[row][col]
if i got right what is inside the $words Array. Your code is a little messi ;)
Try something like this:
$words = array();
if(isset($_POST['submit'])){
// Break down the text as lines:
$lines = explode("\r\n", $_POST['text']);
// For every line...
foreach($lines as $line){
// Seperate the 2 words (seperated by a tab)
$words[] = explode("\t", $line);
}
// Print the result:
var_dump($words);
}
I want To remove duplicate domains in the list of URL ,For example Below is the text file
http://www.exampleurl.com/something.php
http://www.domain.com/something.php
http://www.exampleurl.com/something111.php
http://www.exampleurl.com/something111.php
http://www.exampleurl.com/something222.php
I need to remove duplicate domain and i need below list
http://www.exampleurl.com/something.php
http://www.domain.com/something.php
Below is the code that just remove duplicates in an text file.
$text = array_unique(file($filename));
$f = #fopen("promo1.txt",'w+');
if ($f) {
fputs($f, join('',$text));
fclose($f);
}
?>
Can anyone help me ?
$urls = file('domains.txt');
$uniqueDomains = array_reduce (
$urls,
function (array $list, $url) {
$domain = parse_url($domain, PHP_URL_HOST);
if (!isset($list[$domain])) $list[$domain] = $url;
return $list;
},
array()
);
$uniqueDomains has the hostname as key. If you don't need (and/or want) it use array_values($uniqueDomains);
To compare on the domains, you can use parse_url:
<?php
$text = file_get_contents("input.txt");
$lines = explode("\n",$text);
$filtered_domains = array();
foreach($lines as $line)
{
$parsed_url = parse_url($line);
if(array_search($parsed_url['host'], $filtered_domains) === false)
{
$filtered_domains[$line] = $parsed_url['host'];
}
}
$output = implode("\n", array_keys($filtered_domains));
file_put_contents("output.txt", $output);
?>
<?php
/*
$lines = file('textfile.txt');
*/
$lines = array(
'http://www.exampleurl.com/something.php',
'http://www.domain.com/something.php',
'http://www.exampleurl.com/something111.php',
'http://www.exampleurl.com/something111.php',
'http://www.exampleurl.com/something222.php'
);
foreach($lines as $line){
$url_parsed = parse_url($line);
if(is_array($url_parsed)){
$host = $url_parsed['host'];
if(!#$uniques[$host]){
$uniques[$host] = $line;
}
}
}
echo join('',$uniques);
$f = #fopen("promo1.txt",'w+');
if ($f) {
fputs($f, join("\n",$uniques));
fclose($f);
}
?>
To remove duplicates from an array you can use array_unique(). To make your list an array you can use explode(). Then to make it a string again you can use implode().
To put this all together you can use the following code:
$list = "http://www.exampleurl.com/something.php
http://www.domain.com/something.php
http://www.exampleurl.com/something111.php
http://www.exampleurl.com/something111.php
http://www.exampleurl.com/something222.php";
$newList = implode("\n", array_unique(explode("\n", $list)));
I have a php file that contains some data when checked displays the checked data. How do I increment the variable "$visit" every time it is visited and save it to the text file?
new.php
<html>
<body bgcolor="#99FF00">
<table border="1">
<FORM ACTION="new.php" METHOD="POST">
Enter maximum price <input type="text" name="maximumprice"/>
<p><input type="submit" value="Go" name="Go"/>
</form>
<?
$mPrice = $_POST['maximumprice'];
$file1 = "properties.txt";
$filedata = fopen ($file1, "r");
$array1 = file ($file1);
print "<form action=\"visit.php\" method=\"POST\">";
for ($counter1 = 0; $counter1 < count($array1); $counter1++)
{
$arrLine = $array1[$counter1];
$pCode = getvalue ($arrLine, 0);
$price = getvalue ($arrLine, 1);
$picture = getvalue ($arrLine, 2);
$visit = getvalue ($arrLine, 3);
if ($price < $mPrice)
{
print "<tr>";
print "<td>";
print $pCode. "<br>";
print $price. "<br>";
//print $picture. "<br>";
print $visit. "<br>";
print "<input type=\"checkbox\" name=\"box[]\" value=\"$arrLine\" />";
print "</td>";
print "<td>";
printf("<img src='$picture' width='200' height='150'>");
print "</td>";
print "</tr>";
}
}
print '<input type="submit" name="Visit" value="Visit"/>';
// Move the form outside the for loop
print "</form>";
fclose ($filedata);
function getvalue ($text, $arrNo)
{
$intoarray = explode (",", $text);
return $intoarray[$arrNo];
}
?>
</table>
</body>
</html>
this is the second page, display.php
<html>
<body bgcolor="#99FF00">
<?
foreach ($_POST['box'] as $values)
{
echo "$values <hr/>";
}
?>
</body>
</html>
Step 1: Use a database :-)
No, but really, since you are storing the entire line as the value of the checkbox, you can compare that to the line in the file and update your visit field for matching lines ...
For example, in your processing file:
$filename = "properties.txt";
$data = file($filename, FILE_IGNORE_NEW_LINES);
$checked = $_POST['box'];
foreach($data as $id => $line){
if(in_array($line, $checked)){
//Explode the line into parts
$tmp = explode(',', $line);
//Increment the visit field
$tmp[3]++;
//Put the updated data back in the file data array
$data[$id] = implode(',', $tmp);
//Unset the tmp array
unset($tmp);
}
}
//implode the data back into a text block
$data = implode("\n",$data);
file_put_contents($filename, $data);
This is untested, but should yield what you are looking for ...
As a side note, you do not have to do the fopen call to use the file function. It will open and read the file.
EDIT: Since it appears that visit is the last column in each row and without any flags, the file function will keep the newlines at the end of each line, I added the appropriate flag to the file function.