how do i check for a string occurence in input data - php

I want to check for occurence of a particular string in user input data.
How do I do it.
Here's my code:
<?php
$data = $_POST["data"];
if (strcmp("123",$data))
echo "string matches";
else if (strcmp("234",$data))
echo "string 2 matches";
else
echo"string does not match";
?>

If you want to check to see if $_POST['data'] contains the strings you're searching for, do this:
<?php
$data = $_POST["data"];
if (strpos($data, "123") !== false)
echo "data contains 123";
else if (strpos($data, "234") !== false)
echo "data contains 234";
else
echo "data does not contain either";
But, if you want to check for an exact match, you'd just do:
<?php
//...
if ($data == "123")
echo "data is equal 123";
Check out the manual for strpos() for more information. For a case-insensitive search, you can use stripos() (i for "insensitive") instead.
Checking for strict equality with false is important. If the match begins at the first character in the string, it will return 0, which indicates a match. You need !== to distinguish 0 from false.

Try this,
<?php
//$data = isset($_POST["data"])?$_POST["data"]:'';
$data = "12356789";
if (strpos($data, "123")!== false){
echo "string matches";
}
else if (strpos($data, "234")!== false){
echo "string 2 matches";
} else {
echo"string does not match";
}
?>

Try this one:
<?php
$data = $_POST["data"];
if (strcmp("123",$data) === 0)
echo "string matches";
else if (strcmp("234",$data))
echo "string 2 matches";
else
echo"string does not match";
?>

Related

How to check if a fixed string contain any additional character?

This code check if "have" is present inside the string, assuming the string always begin with "I have found" what i need is a function that check if the string contain "I have found" plus something else. Example: I have found 500. where 500 can be anything, and unknow.
$a = 'I have found';
if (strpos($a, 'have') !== false) {
echo 'true';
}
If you want to know what has been found:
function get_found($str){
if(strpos($str, "I have found")===false)
return "nothing";
$found = trim(substr($str, strlen("I have found")));
if($found == "")
return "nothing";
return $found;
}
echo get_found("I have found a friend"); //outputs "a friend"
echo get_found("I have found"); //outputs "nothing"
You can use preg_match(), like in this code:
$a = 'I have found'; //fixed string
$str = 'I have found 500';
if (preg_match('/^'.$a.'(.+?)$/', $str, $m)){
echo 'The string contains additional: '.$m[1];
}
else echo 'String fixed';

How to match Russian words using preg_match php

I want to match the Russian words using preg_match in php
i tried below examples but it is not working
"/\b".$word."\b/i"
'/(?<!\pL)'.$word.'(?!\pL)/iu'
/\b'.$word.'\b/iu
'/'.$find_ru.'/iu'
e.g
Input
$word = "королевства";
$russian_string = "источники в спецслужбах королевства";
if(preg_match('/'.$word.'/iu', $russian_string)){
echo "Matched";
}else{
echo "Not Matched";
}
Missing closing bracket in if condition
$word = "королевства";
$russian_string = "источники в спецслужбах королевства";
if(preg_match('/'.$word.'/iu', $russian_string, $word)){
echo "Matched";
}else{
echo "Not Matched";
}
Output will be "Matched"
Or you can do like this way also,
$haystack = "источники в спецслужбах королевства";
$needle = "королевства";
if( strpos( $haystack, $needle ) !== false ) {
echo "Matched";
}else{
echo "Not Matched";
}
Output will be "Matched"

PHP Search for a string in remote webpage

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.

Does strpos have an issue with a string that has html in it?

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';
}

How to put URL in array and search through array for matching string?

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.";
}
?>

Categories