On pressing the submit button, I want the PHP code to check to see if a string exists within a webpage on another website.
$random = rand(100,1000);
strpos(file_get_contents('website.com/url.php?username='.$username), $random);
So, until the code has managed to find the value of $random, it won't continue with the rest of the script. The updates can take about 10 seconds.
I have tried a while script:
$random = rand(100,1000);
echo '<div data-alert class="alert-box alert">'.$random.'</div>';
$key = false;
while($key){
if(strpos(file_get_contents('website.com/url.php?username='.$username), $random)) $key = true;
}
This doesn't seem to work.
On pressing the submit button, I want the PHP code to check to see if a string exists within a webpage on another website.
If I understood your question correctly, try this:
$content = file_get_contents($url);
if(str_replace($string, "", $content) != $content)
echo "found";
EDIT:
do {
$content = file_get_contents($url);
} while(strpos($content, $string) === false);
EDIT 2:
$i = 0;
do {
$content = file_get_contents($url);
$i++;
} while(strpos($content, $string) === false && $i <= 100);
EDIT 3:
$i = 0;
do {
$content = file_get_contents($url);
$i++;
} while(strpos($content, $string) === false && $i <= 100);
if($i > 100){
echo "nothing found";
}
EDIT 4:
$startTime = time();
do {
$content = file_get_contents($url);
} while(strpos($content, $string) === false && time() - $startTime < 30);
if(time() - $startTime > 30){
echo "nothing found";
}
Related
I have text file
name,name1
willhaveishere1
name,name2
willhaveishere2
name,name3
willhaveishere3
i want read it and return like that
$nn = name1
$ss = willhaveishere1
with my code i get only name1
my code is
$file1 = "file.txt";
$file = file($file1);
$count = count($file);
if($count > 0) {
$i = 1;
foreach($file as $row) {
$n = strstr($row, 'name,');
$cc = array("name,");
$dd = array("");
$nn = str_replace($cc, $dd, $n);
echo $nn;
$i++; } }
This is probably what you need
if($count > 0) {
foreach($file as $row) {
$pos = strpos($row, ',');
if($pos !== false){
echo substr($row, $pos + 1);
$nn[] = substr($row, $pos + 1);
} else {
echo $row;
$ss[] = $row;
}
}
}
EDIT
Yes, just loop through, but make sure both $nn and $ss has same count, which is depending on your file.
Also Note: mysql_* functions has been deprecated, so please use mysqli or PDO instead
$count = count($nn);
for($i=0; $i < $count; $i++){
$sql = "INSERT INTO users(name, line) VALUES('$nn[$i]', '$ss[$i]')"; mysql_query($sql);
}
EDIT 2
try this example:
$file = array(
0 => 'name,name1',
1 => 'willhaveishere1',
2 => 'name,name2',
3 => 'willhaveishere2',
4 => 'name,name3',
5 => 'willhaveishere3'
);
$count = count($file);
if($count > 0) {
foreach($file as $row) {
$pos = strpos($row, ',');
if($pos !== false){
$nn[] = substr($row, $pos + 1);
} else {
$ss[] = $row;
}
}
}
echo '<pre>';
$count = count($nn);
for($i=0; $i < $count; $i++){
$sql = "INSERT INTO users(name, line) VALUES('$nn[$i]', '$ss[$i]');";
echo $sql.PHP_EOL;
}
You can try this straightforward method:
if($fh = fopen("file.txt","r")){
$nameBefore = false;
//loop through every line of your file
while (!feof($fh)){
$line = fgets($fh);
//check if the name was detected in previous line
if ($nameBefore !== false)
{
//you have the set of name and line, do what you want
echo $nameBefore . ': ' . $line . '<br />';
$nameBefore = false;
}
else
{
//see if the line is made of two coma separated segments and the first one is 'name'
//Remember the name for the next line
$parts = explode(',', $line);
if (count($parts) == 2 && $parts[0] == 'name')
$nameBefore = $parts[1];
}
}
fclose($fh);
}
One option is to use strpos to find the first occurrence of the character in the line, and if found remove everything from the line before that position. This way you are left with only the part of the line you are interested in.
Code:
$character = ',';
$fileHandle = fopen('file.txt', 'r');
while (!feof($fileHandle)) {
// Retrieve the line from the file
$line = fgets($fileHandle);
// If the line contains the character
// Remove everything before the character
$charPos = strpos($line, $character);
if ($charPos !== false) {
$line = substr($line, $charPos + 1);
}
// Do something with the remainder of the line
echo $line . PHP_EOL;
}
fclose($fileHandle);
Output:
name1
willhaveishere1
name2
willhaveishere2
name3
willhaveishere3
If you wish to retrieve the following line, simply do another retrieve line call in your loop:
while (!feof($fileHandle)) {
// Retrieve two lines in one loop iteration
$lineOne = fgets($fileHandle);
$lineTwo = fgets($fileHandle);
}
Making sure to only apply the comma replace part on the first line. This can lead to problems though if your data is... inconsistent.
Hope this helps.
I'm taking data from an excel using phpexcel, like this:
$number = $objPHPExcel->getActiveSheet()->getCell('A3')->getValue();
number is clearly a number, okay? so, after that I want to know if $number exists on the word $elem:
if(strpos($elem,$number) !== false) //está?
{
$answer = true;
}
the problem is that when I test it with this data, the $answer is true, and it shouldn't be:
$number = 11001456
$elem = '10001033.jpg'
So... what's wrong here?
PD: I'm going to post the entire code so you can see it, If I try to do (string)$number then the code crashes, it exceeds time execution.... (using cakephp)
The important thing is located at the function SearchPhoto... you will see the strpos there...
public function admin_load() //esto sirve para cargar un excel...
{
if($this->request->is('post'))
{
$data = $this->request->data;
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
date_default_timezone_set('Europe/London');
/** Include PHPExcel_IOFactory */
require_once WWW_ROOT . '/excelWorker/Classes/PHPExcel/IOFactory.php';
echo date('H:i:s') , " Load from Excel2007 file" , EOL;
$callStartTime = microtime(true);
$objPHPExcel = PHPExcel_IOFactory::load($data['People']['excel']['tmp_name']);
$dir = WWW_ROOT . "img/photos";
$files = scandir($dir);
$batchPeople = array();
for($i = 2; $i <= $data['People']['num']; $i++)
{
$batchPeople[$i-2]['People']['fullname'] = $objPHPExcel->getActiveSheet()->getCell('B'.$i)->getValue();
$batchPeople[$i-2]['People']['floor'] = $objPHPExcel->getActiveSheet()->getCell('C'.$i)->getValue();
$batchPeople[$i-2]['People']['country'] = $objPHPExcel->getActiveSheet()->getCell('D'.$i)->getValue();
$batchPeople[$i-2]['People']['day'] = $objPHPExcel->getActiveSheet()->getCell('F'.$i)->getValue();
$batchPeople[$i-2]['People']['month'] = $objPHPExcel->getActiveSheet()->getCell('G'.$i)->getValue();
$batchPeople[$i-2]['People']['photo'] = $this->SearchPhoto($objPHPExcel->getActiveSheet()->getCell('A'.$i)->getValue(),$files);
}
// $this->People->saveMany($batchPeople);
}
}
function SearchPhoto($number, $array)
{
$answer = '';
$getOut = false;
foreach($array as $elem)
{
if(strcmp($elem,'.') != 0 && strcmp($elem,'..') != 0)
if(strpos($elem,$number) !== false) //está?
{
echo 'coinciden--> '. $number . ',' . $elem;
echo '<br>';
$answer = $elem;
$getOut = true;
}
if($getOut)
break;
}
return $answer;
}
This should work for you:
(BTW: You use $number in the code and not $num)
<?php
$num = 11001456;
$elem = "10001033.jpg";
if(strpos($elem, (string)$num) !== false) {
echo "yes";
}
?>
For more information about strpos() look into the manual: http://php.net/manual/en/function.strpos.php
And a quote from there:
needle:
If needle is not a string, it is converted to an integer and applied as the ordinal value of a character.
echo chr($number); // p
echo strpos($elem, 'p'); // 10 (which is p in $elem)
I ended up using preg_match to solve my problem... I still don't know what's wrong with using strpos... because it works on all the sites I made expect this particular case!
In case anyone wondering what's the exact solution:
$text = $B;
$pattern = '/'.$A.'/';
preg_match($pattern,$text,$matches);
if(isset($matches[0]))
$answer = true;
I am building a script thats goal is to check up to 100 URLS for validity (No 404).
The only variable in the URL is the page number, like so:
http://example.com/category/id/products/page/1
http://example.com/category/id/products/page/2
and so on up to 100,
as soon as my code reaches an invalid URL, I want it to stop and echo the number it has reached, this is the code I am trying to no avail:
$url ="http://example.com/category/id/products/page/1";
if (false !== strpos($url, $id)) {
$pageNumber = 2;
$check = true;
do{
$urlIterate = "http://example.com/category/id/products/page/".$pageNumber;
if(false !== strpos($urlIterate, $id)){
$pageNumber++;
}
else{
$check = false;
}
}
while($pageNumber <= 99);
}
else{
$check = false;
echo 'No pages were found at all';
}
echo "There were ". $pageNumber." pages.;
?>
Im not sure if this is what youre looking for, but try this:
<?php
$id_to_search = "90";
for ($i = 1; $i <= 100; $i++) {
$url = "http://example.com/category/id/products/page/" . $i;
$values = parse_url($url);
$paths = explode('/', $values['path']);
$id_from_url = $paths[5];
if ($id_to_search === $id_from_url) {
$headers = get_headers($url);
if ($headers[0] == 'HTTP/1.0 404 Not Found') {
echo "URL Found! URL is invalid(404). URLs searched = " . $i . "<br>";
} else {
echo "URL is valid<br>";
}
} else {
echo "URL was searched but it does not match the ID we are looking for<br>";
}
}
Why are you not using the for loop? It will be better while we know how much iterations will we need.
for($i = 1; $1<=100; $i++){
$urlIterate = "http://example.com/category/id/products/page/".$i; //generate url
$headers = get_headers($urlIterate, 1); //get headers
if($headers[0] != 'HTTP/1.1 200 OK'){ //if we have an error
if($i > 1) //if there was at least one found
echo 'Last found number is ' . ($i-1);
else
echo 'No pages were found at all';
break; //stops the 'for' loop
}
}
Your code is looking for $id in urls - what's the point?
guys. Please help me to find out the problem. I have one script on my ftvgirlsbay.com site:
<?php
define('C', 16); // NUMBER OF ROWS
define('D', 6); // NUMBER OF CELLS
$file = #file_get_contents('http://ftvgirlsbay.com/design/tables/gals.txt');
if($file === FALSE) {
echo "<h2>Make file gals.txt with data!</h2>";
return FALSE;
}
$file = explode("\r\n", $file);
$thumbs = array();
while(list(,$el) = each($file)) {
$el = explode('|', $el);
if(isset($el[0]) && isset($el[1]))
$thumbs[] = array($el[0], $el[1]);
}
//print_r($thumbs);
$size = sizeof($thumbs);
if($size < C*D) {
echo "<h2>More data needed. You have $size, you need ".C*D."!</h2>";
return FALSE;
}
$range = range(0, $size - 1);
shuffle($range);
//print_r($range);
?>
<table align=center>
<?php
$num = 0;
for($i = 0; $i < C; $i++) {
echo '<tr align=center>'."\r\n";
for($k = 0; $k < D; $k++) {
echo '<td>FTV '.$thumbs[$range[$num]][0].' Gallery </td>'."\r\n";
$num++;
}
echo '</tr>'."\r\n";
}
?>
</table>
Result of this script:
"More data needed. You have 1, you need 96!"
( http://ftvgirlsbay.com/random_scripts/test.php )
If this part
$file = #file_get_contents('http://ftvgirlsbay.com/design/tables/gals.txt');
change to
$file = #file_get_contents('http://sexsticker.info/ftvgirlsbay.com/pictable/gals.txt');
it works just fine
( http://ftvgirlsbay.com/random_scripts/test2.php )
So if I links to the .txt file on my server's side script reads only 1 line.
If I links to the .txt on the remote server - script works wilh all lines
I figured out your problem and was able to replicate the issue.
The issue is with the \r in
$file = explode("\r\n", $file);
Remove the \r
$file = explode("\n", $file);
Both servers might be different. One may be on a Linux, while the other on a Windows server.
I'm using this script to list a few Twitch.tv streams and their status (offline or online).
If there are no online streams found, I want it to display a text saying that all are offline.
Code that checks if the added streams are online:
//get's member names from stream url's and checks for online members
$channels = array();
for ($i = 0; $i < count($members); $i++) {
if (isset($json_array[$i])){
$title = $json_array[$i]['channel']['channel_url'];
$array = explode('/', $title);
$member = end($array);
$viewer = $json_array[$i] ['stream_count'];
onlinecheck($member, $viewer);
$checkedOnline[] = signin($member);
}
}
unset($value);
unset($i);
//checks if player streams are online
function onlinecheck($online, $viewers)
{
//If the variable online is not equal to null, there is a good change this person is currently streaming
if ($online != null)
{
echo ' <strong>'.$online.'</strong>';
echo '  <img src="/images/online.png"><strong> Status:</strong> Online! </br>';
echo '<img src="/images/viewers.png"><strong>Viewers:</strong>  ' .$viewers.'</br>';
}
}
Full code:
<html>
<head>
<title>Streamlist</title>
</head>
<body>
<?php
$members = array("ncl_tv");
$userGrab = "http://api.justin.tv/api/stream/list.json?channel=";
$checkedOnline = array ();
foreach($members as $i =>$value){
$userGrab .= ",";
$userGrab .= $value;
}
unset($value);
$json_file = file_get_contents($userGrab, 0, null, null);
$json_array = json_decode($json_file, true);
$channels = array();
for ($i = 0; $i < count($members); $i++) {
if (isset($json_array[$i])){
$title = $json_array[$i]['channel']['channel_url'];
$array = explode('/', $title);
$member = end($array);
$viewer = $json_array[$i] ['stream_count'];
onlinecheck($member, $viewer);
$checkedOnline[] = signin($member);
}
}
unset($value);
unset($i);
function onlinecheck($online, $viewers) {
if ($online != null) {
echo ' <strong>'.$online.'</strong>';
echo '  <img src="/images/online.png"><strong> Status:</strong> Online! </br>';
echo '<img src="/images/viewers.png"><strong>Viewers:</strong>  ' .$viewers.'</br>';
}
}
$alloffline = "All female user streams are currently offline.";
function signin($person){
if($person != null){
return $person;
}
?>
</body>
</html>
............................................................................................................................................................................
Is it because your $userGrab URL contains usernames twice? This is the URL whose contents you're retrieving:
http://api.justin.tv/api/stream/list.json?channel=painuser,ZombieGrub,Nathanias,Youbetterknowme,ncl_tv,painuser,ZombieGrub,Nathanias,Youbetterknowme,ncl_tv
Having looked at the response, it doesn't look like it's causing the problem. The strange URL is a result of you appending to the $userGrab string in the first foreach loop, after you've already added them with the implode() function call before. I think twitch.tv is rightly ignoring duplicate channels.
If all the values in $checkedOnline are null, everyone is offline. Put this at the end of your first code sample:
$personOnline = false;
foreach($checkedOnline as $person) {
if($person !== null) {
$personOnline = true;
break;
}
}
if(!$personOnline) {
echo 'No one is online';
}
else {
//there is at least someone online
}