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.
Related
I am trying to check for a word in every created sting.
So I am using "if" after "if" in order to check the word in every sting but also if the word is there to print out every thing in which find it.
But i want if the searched word is not found in ANY of the strings to go to else statement No such word.
But now the code posted "no such word" even if Only ONE of the "IFs" is not ok.
Here is the code:
if(stripos($str, $text)){
/*echo $str ;*/ echo $str = preg_replace("/\b([a-z]*${fwcode}[a-z]*)\b/i","<font color ='red'><b>$1</b></font>",$str);
echo "</br>";
}
if(stripos($str2, $text)){
/*echo $str2 ;*/ echo $str2 = preg_replace("/\b([a-z]*${fwcode}[a-z]*)\b/i","<font color ='red'><b>$1</b></font>",$str2);
}
if(stripos($str3, $text)){
/*echo $str3 ;*/ echo $str3 = preg_replace("/\b([a-z]*${fwcode}[a-z]*)\b/i","<font color ='red'><b>$1</b></font>",$str3);
}
if(stripos($str4, $text)){
/*echo $str4 ;*/ echo $str4 = preg_replace("/\b([a-z]*${fwcode}[a-z]*)\b/i","<font color ='red'><b>$1</b></font>",$str4);
}
else
{
echo "No such word"; *** Now will print it if cannot find the $text in ANY of the $str -ings
}
$str,$str1,$str2... are the strings. $text-> is the variable for search.
Please tell me how to have all the strings checked and displayed if the searched word is there, and if in none of the is found Only Then to have else executed.
Thank you.
I tried to put If statement in IF but didn't work.
Please tell me how to have all the strings checked and displayed if the searched word is there, and if in none of the is found Only Then to have else executed.
Thank you.
Your else statement from your code example only applies to the last if:
$arr_string = [
'string1',
'string2',
'string3',
'string4',
];
$found = false;
foreach ($arr_string as $value) {
if(stripos($value, $text) !== false) {
echo preg_replace("/\b([a-z]*${fwcode}[a-z]*)\b/i","<font color ='red'><b>$1</b></font>",$value);
$found = true;
}
}
if (!$found) {
echo "No such word";
}
This way you can easily check as many strings as you want. Using a loop like foreach or for.
Also read about the use of stripos: https://www.php.net/manual/en/function.stripos.php
This function may return Boolean false, but may also return a
non-Boolean value which evaluates to false.
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.
strpos is not finding >$2.99<. I have echoed $html right before the if statement, and I can find >$2.99< in it, but the result is not found.
$webpage = file_get_contents ($itunesurl);
$html = htmlspecialchars ($webpage);
echo $html;
if(strpos($html, '>$2.99<') !== FALSE) {
echo 'found';
}
else {
echo 'not found';
}
Search $webpage rather than $html, since the > and < characters get converted to entities by htmlspecialchars.
if(strpos($webpage, '>$2.99<') !== FALSE) {
echo 'found';
}
else {
echo 'not found';
}
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.";
}
?>
$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];
}