php How to include the first line in a file txt - php

There is a problem when the search does not include the first line. What is the method to include the first line?
$name = trim($_GET['name']);
$file = file('data.txt');
for($i = 0; $i < count($file); $i++){
$read = explode('|', $file[$i]);
if($name == $read[0]){
$found = 1;
$t1 = $read[0];
$t2 = $read[1];

Using the following data as sample (data30nov.txt) - I have added a few lines to demonstrate that it can search line 1 and other lines
a|125|hh#hh.com
g|653|uu#uu.com
vb|603|uu#uu.com
a|1244|hh#hh4444.com
d|124|hh#hh2.com
Assuming that you are searching the string "a"
Your code should be able to search the 1st line
See the code below
<?php
//$name = trim($_GET['name']);
$name = "a";
$file = file('data30nov.txt');
for($i = 0; $i < count($file); $i++){
$read = explode('|', $file[$i]);
if ($name == $read[0]){
$found = 1;
echo $file[$i]. "<br>";
}
}
?>
The output can be found here:
http://www.createchhk.com/SO/testSO30Nov2021.php
(It shows clearly that the program can search the 1st line)

Related

PHP ZipArchive list only one level of files/folders

I have wp.zip and would like to list only one level of files/folders. My current code:
$zip = new \ZipArchive();
$zip->open('wp.zip'), \ZipArchive::RDONLY);
for ($i = 0; $i < $zip->numFiles; $i++) {
$stat = $zip->statIndex($i);
echo $stat['name'] . ' ';
}
This code spits out entire list of files recursively.
I need only first level, like this:
wp-admin/
wp-content/
index.php
config.php
<...>
What's the best approach to achieve my goal?
Yes, you can only parse the name and get the level of the file accordingly.
<?php
$zip = new \ZipArchive();
$zip->open('wp.zip');
function getEntryList($zip, $level = 1){
$res = [];
for($i = 0; $i < $zip->numFiles; ++$i){
$name = explode("/", trim($zip->statIndex($i)['name'],"/"));
if(count($name) == $level + 1){
$res[] = end($name);
}
}
return $res;
}
print_r(getEntryList($zip));

Why this PHP code does not run - Code fitter Mail

I have code
require_once('verify.php');
$file = 'mail.txt';
$allfile = file($file);
for ($i = 0; $i <= 10; $i++) {
$cuong = $allfile[$i];
$dep = 'admin#ngocuong.net';
$kq = verifyEmail($cuong, $dep);
if($kq == '2') {
echo 'OK<br />';
} else {
echo 'Nope<br />';
}
}
echo 'Done!!!';
with $cuong = 'cuong.fl310#gmail.com'; code run. Return is OK.
But i put all my list email into file mail.txt. And run code read line in file txt. $cuong = $allfile[$i] code not run.
I had show
for($i = 0; $i <= 10; $i++) {
$cuong = $allfile[$i]
echo $cuong;
}
it print list email in file txt. With $i= 0; $cuong = 'cuong.fl310#gmail.com' But code not run. Return enqual: Nope !!!
Plz help me fix it.
Thanks for readding!!!

warning occuring in php word occurence

I have written the following code to count the number of string occurrences in a given file.
PHP
<?php
$p = fopen("g.txt", "r");
$q = fread($p, filesize("g.txt"));
$t = explode(" ", $q);
$m = explode(" ", $q);
$i = 0;
$j = 0;
$r = 0;
$count = 0;
$c = count($t);
$d = array();
echo "count of".
"<br/>";
for ($i = 0; $i < $c; $i++) {
for ($j = $i; $j < $c; $j++) {
if ($t[$i] == $t[$j]) {
$count = $count + 1;
}
}
for ($r = $i + 1; $r < $c; $r++) {
if ($t[$i] == $t[$r])
unset($t[$r]);
}
echo $t[$i].
"=".$count.
"<br/>";
$count = 0;
}
?>
I am getting a notice of undefined offset on line numbers 17 and 24, though my output is coming out to be correct. Can you please help me in rectifying the above problem?
The problem is that you are deleting items from the array $t. You saved the count in $c, but the actual count will change by your last inner loop.
Even if you replace $c by count($t) everywhere, it will go wrong, because the last loop should be in reverse order, otherwise you skip items. For instance if you have the list 'a', 'b', 'c'. then when you delete 'b' and increment $r, you will not check 'c' at all.
So, if I fix those things, your code becomes as below. Although I didn't really check it for other issues. Frankly, I don't really get what is should do. ;-)
<?php
$p=fopen("g.txt","r");
$q=fread($p,filesize("g.txt"));
$t=explode(" ",$q);
$m=explode(" ",$q);
$i=0;
$j=0;
$r=0;
$count=0;
$d=array();
echo "count of"."<br/>";
for($i=0; $i<count($t); $i++)
{
for($j=$i; $j<count($t); $j++)
{
if($t[$i]==$t[$j])
{
$count=$count+1;
}
}
for($r=count($t) - 1; $r > $i; $r--)
{
if($t[$i]==$t[$r])
unset($t[$r]);
}
echo $t[$i]."=".$count."<br/>";
$count=0;
}
?>
In conclusion, you should do more tests. If the outcome of this script was okay, then it was by accident.

How to output arrays in a certain way using PHP?

I'm try to print an array where every other value is reassigned, as examples (from this):
17.34502870451717,62.46137370987033
To this:
62.46137370987033,17.34502870451717
That part have I succeeded with, but now I have this structure:
[62.46137370987033,[17.34501402936927,]
[62.46123453616544,[17.34525377433593,]
[62.4610178881864,[17.34546663705899,]
This is where I get stuck and do not know how to write.
The structure I want looks like this:
[62.392628, 17.309413],
[62.393162, 17.309193],
[62.393403, 17.30922]
Here is my explode.php (GIST)
<?php
$dwarf = "17.34502870451717,62.46137370987033,17.34501402936927,62.46123453616544";
$minion = explode(",",$dwarf);
$wing = "[";
for ($i = 0;$i < count($minion) -1; $i++) {
echo $wing . $minion[$i+1].",";
if($i%2==1) { echo "]<br />"; }
} echo $minion[0] . $wing;
?>
As I understand it, as long as there's always even pairs it should be as easy as;
<?php
$dwarf = "17.34502870451717,62.46137370987033,17.34501402936927,62.46123453616544";
$minion = explode(",",$dwarf);
$eol = '';
for ($i = 0;$i < count($minion) -1; $i+=2) {
echo $eol.'['.$minion[$i+1].','.$minion[$i]."]";
$eol=',<br/>';
}
echo '<br/>';
>>> [62.46137370987033,17.34502870451717],
>>> [62.46123453616544,17.34501402936927]
Try This
$dwarf = "17.34502870451717,62.46137370987033,17.34501402936927,62.46123453616544";
$minion = explode(",",$dwarf);
$wing = "[";
for ($i = 0;$i < count($minion) -1; $i+=2)
{
echo $kk = $wing . $minion[$i+1].",".$minion[$i]."],<br>";
}
Just a small modification to the given answers
$dwarf = "17.34502870451717,62.46137370987033,17.34501402936927,62.46123453616544";
$minion = explode(",",$dwarf);
$str = '';
for ($i = 0;$i < count($minion) -1; $i+=2) {
$str.='['.$minion[$i+1].','.$minion[$i].'],<br/>';
}
echo rtrim($str,','); // to trim ',' at the end

defining alphabets as numbers not working inside loop

Please check my code below,it returns 0 while I am expecting a result 14.But when I add A+D manually it returns 5.Am i doing something wrong inside the loop ?
<?php
define('A',1);
define('B',2);
define('C',3);
define('D',4);
define('E',5);
//echo A+D; returns 5
$name = 'EACE';
$len = strlen($name);
for($i = 0; $i<=$len; $i++)
{
$val += $name[$i];
}
echo $val; //returns 0
?>
You need to use constant(..) to get the value of a constant by name. Try this:
for ($i = 0; $i < strlen($name); $i++) {
$val += constant($name[$i]);
}
define('A',1);
define('B',2);
define('C',3);
define('D',4);
define('E',5);
//echo A+D; returns 5
$name = 'EACE';
$len = strlen($name);
$val = null;
for($i = 0; $i<=$len-1; $i++)
{
$val += constant($name[$i]);
}
echo $val;

Categories