I am trying to write a script with PHP where it'll open up a text file ./urls.txt and check each domain for a specific word. I am really new to PHP.
Example:
Look for the word "Hello" in the following domains.
List:
Domain1.LTD
Domain2.LTD
Domain3.LTD
and just simply print out domain name + valid/invalid.
<?PHP
$link = "http://yahoo.com"; //not sure how to loop to read each line from a file.
$linkcontents = file_get_contents($link);
$needle = "Hello";
if (strpos($linkcontents, $needle) == false) {
echo "Valid";
} else {
echo "Invalid";
}
?>
$arrayOfLinks = array(
"http://example.com/file.txt",
"https://www.example-site-2.com/files/file.txt"
);
$needle = "Hello";
foreach($arrayOfLinks as $link){ // loop through the array
$linkcontents = file_get_contents($link);
if (stripos($linkcontents , $needle) !== false) { // stripos is case-insensitive search
// the needle exists in $linkcontents
// !== false instead of != false since stripos can return 0 meaning the needle is the first word of the contents
echo "Valid";
} else {
// the word does not exist in the given text
echo "Invalid";
}
}
First of all use CURL in favor of file_get_contents() because of security.
This would be a correct strpos example:
//your previous code
if (strpos($linkcontents, $needle) !== false) { // see the !==
echo "Valid"; //Needle found
} else {
echo "Invalid"; //Needle not found
}
For more complex crawling you could use some regexp instead of strpos.
Related
I have a comma separated list and in that list i am interested to know if a specific string that starts in a certain way is present.
$accounting = 'acc';
$str = 'loan,k,hi,588888,acc';
if (strpos($str, $accounting) === TRUE)
{
echo 'that contained accounting';
}
else{
echo 'nothing was found';
}
The code is giving me nothing is found.Does strpos work in a comma delimited list?.
Does strpos work in a comma delimited list?.
No, it doesn't, because $str is not a list, it's just a string. You have to convert it to a list (=array) first:
$lst = explode(',', $str);
and then search this list:
if(in_array('acc', $lst)....
Your wording is a bit unclear, but if you're looking for a list element that starts with a specific string, it's more complicated:
function has_element_that_starts_with($lst, $prefix) {
foreach($lst as $item)
if(strpos($item, $prefix) === 0) // note three ='s
return true;
return false;
}
Another option is a regular expression:
if(preg_match("~(^|,){$acc}(,|$)~", $str)....
for partial strings:
if(preg_match("~(^|,){$acc}~", $str)....
Your code is right change === to ==. will work.
$accounting = 'acc';
$str = 'loan,k,hi,588888,acc';
if (strpos($str, $accounting) == TRUE)
{
echo 'that contained accounting';
}
else{
echo 'nothing was found';
}
Use php strstr() , Reference
$accounting = 'acc';
$str = 'loan,k,hi,588888,acc';
if (strstr($str, $accounting) )
{
echo 'that contained accounting';
}
else{
echo 'nothing was found';
}
header('Content-Type: text/html; charset=utf-8');
include 'simple_html_dom.php';
$html = file_get_html('http://www.wettpoint.com/results/soccer/uefa/uefa-cup-final.html');
$cells = $html->find('table[class=gen] tr');
foreach($cells as $cell) {
$pre_edit = $cell->plaintext . '<br/>';
echo $pre_edit;
}
$pos = strpos($pre_edit, "Tennis");
var_dump($pos);
if ($pos == true) {
echo "string found!";
}
else
{
echo "string not found";
}
When I search for the string "Tennis" PHP returns "string not found". It only returns "string found" if I search for a string that belongs to the last iteration of the foreach with length=149 (ignoring the first five lines of the $pre_edit var). Could you please give me some advice as to how to resolve this? Thank you!
You're not doing your search inside the foreach() loop, so you'll only EVER get the last node retrieved by the loop.
If you'd properly indented the code, you'd see the problem. It should be:
foreach($cells as $cell) {
$pre_edit = $cell->plaintext . '<br/>';
echo $pre_edit;
$pos = strpos($pre_edit, "Games");
var_dump($pos);
if ($pos !== false) {
echo "string found!";
} else {
echo "string not found";
}
}
Right now you've got:
foreach($cells as $cell) {
blah blah
}
if (strpos(...))) {
blah blah
}
Also note that I've changed $pos == true to $pos !== false. strpos can and will return a 0 if the string you're searching for is at the beginning the of string. But in PHP, 0 == false is TRUE, but 0 === false is FALSE. You need to use the strict equality test, which compares types AND values, to check for the boolean FALSE that strpos returns when the search fails.
I am trying to search through a URL for a matching string, but the below code snippet doesn't seem to work.
<?php
$url = "http://www.drudgereport.com";
$search = "a";
$file = file($url);
if (in_array($search,$file)) {
echo "Success!";
} else {
echo "Can't find word.";
}
?>
If you are just searching for an occurrence of a string on the page, you can use
$str = file_get_contents($url);
if (strpos($str, $search) !== false) {
echo 'Success!';
} else {
echo 'Fail';
}
in_array() checks if an array member is equal to your needle.
It is improbable many websites will have a line which is equal to a only.
Also, is allow_url_fopen enabled?
That code will only find a line that has the exact $search string (likely including whitespace). If you're parsing HTML, check PHP's DOMDocument classes. Or, you can use a regex to pull what you need.
As #alex says, check is allow_url_fopen is enabled.
Also you can use strpos to search the string:
<?php
$url = "http://www.drudgereport.com";
$search = "a";
$file_content = file_get_contents($url);
if (strpos($file_content, $search) !== false) {
echo "Success!";
} else {
echo "Can't find word.";
}
?>
When someone writes:
"Near Tokyo"
I would like to check first if the $search contains "near" and if it does then take the "Tokyo" into a variable $location.
I tried this:
if(strpos($search, 'near') == true){
$search = explode("near ", $location);
echo $location;
exit();
}
did not work, it does not execute the if statement
You have multiple bugs here:
strpos may return 0, which signifies a match but will not compare equal to true
strpos is case-sensitive, which would make your example not work (look into stripos instead)
explode is also case-sensitive
It would probably be easiest to use a regex for this:
$input = "Near Tokyo";
if (preg_match('/near\s+(\w+)/i', $input, $matches)) {
echo "Near: ".$matches[1]."\n";
}
else {
echo "No match.\n";
}
See it in action.
This particular regex will only match the next word after "near", but this can be modified to suit your requirements.
returntype of strpos is int, not bool
http://php.net/manual/en/function.strpos.php
so use (according to manual pages) this:
if(strpos($search, 'near') !== false)
change it to:
if(strpos($search, 'near') !== false){
$search = explode("near ", $location);
echo $location;
exit();
}
just take a look at the documentation where this behaviour is explained:
It is easy to mistake the return values for "character found at
position 0" and "character not found". Here's how to detect the
difference:
<?php
$pos = strrpos($mystring, "b");
if ($pos === false) { // note: three equal signs
// not found...
}
?>
Yes, strpos, cumbersome boolean result handling. You probably should or should want to use stristr instead, which is also case-insensitive:
if (stristr($search, "Near")) {
And since you are extracting text anyway, why not use a regex? (People are using the awful explode workaround way too often.)
if (preg_match("'Near (\S+)'i", $search, $match)) {
echo $match[1];
}
use this to get a result:
if(strpos($search, 'near') !== false){
$location = explode("near ", $search);
print_r($location);
exit();
}
$search = "Near Tokyo";
if(strpos($search, 'Near') === 0){
$location = explode("Near ", $search);
echo $location[1];
exit();
}
<?php
$search = "Near Tokoyo";
if(preg_match("/near ([a-z]+)/i", $search, $match))
{
$location = $match[1];
echo $location;
}
?>
EDIT:
Fixed some bugs in your code.
if(stripos($search, 'near') !== false){
$location = explode("near ", $search);
echo $location[0];
exit();
}
$bodytext = "we should see this text <more> but not this at all <html>";
if(stristr($bodytext, "<more>") == TRUE)
{
$find = "<more>";
$pos = stripos($bodytext, $find);
$bodytext = substr($bodytext, 0, $pos);
}
echo "$bodytext";
If the $bodytext contains other html code, this also causes the above code to return true :
<more
more>
How do I adjust my code so only (and exactly) :
<more>
returns true?
Simple/naiive:
$bodytext = preg_replace('/(.*?)<more>.*/', $1, $bodytext);
stristr returns all of the string from the match to the end of the string. If a match is not found, it returns false.
You therefore need to do this:
if(stristr($bodytext, "<more>") !== false) {
// match found
}
stripos is more suited to your needs:
$pos = stripos($bodytext, "<more>");
if($pos !== false) {
// match found
}
Alternative: See Marc B's answer, which does everything you appear to be trying to achieve in a single statement.
You could also use the explode function and output the first element of the array
$bodytext = "we should see this text <more> but not this at all <html>";
if(stristr($bodytext, "<more>") == TRUE)
{
$split = explode('<more>', $bodytext);
echo $split[0];
}