I am a new person in the PHP. I have one String.I use this to find a word for strpos function
for example..
$a= "google" ;
$b= "google is best one" ;
if(strpos($b, $a) !== false) {
echo "true, " ; // Working Fine...
}
so i want to in this case check My Example
$a= "google,yahoo,Bing" ;
$b= "Bing is Good " ;
if(strpos($b, $a) !== false) {
echo "true, " ; // I Need True...
}
This is how you do PHP
Split the string on the comma using explode() and then check each of the parts individually:
$a= "google,yahoo,Bing" ;
$b= "Bing is Good " ;
$parts = explode(',', $a);
foreach ($parts as $part) {
if(strpos($b, $part) !== false) {
echo "true, " ; // I Need True...
}
}
To clarify, it looks like you're trying to perform a match such that any value in a list of values from $a is present in $b. This is not possible with a simple strpos() call because strpos() looks for the exact occurrence of the string you're passing in.
What you're attempting to do is a pattern-based search. To make this work, please look into using a regular expression with preg_match(). Such an example could look like this:
$pattern = '/google|yahoo|Bing/';
$target_string = 'Bing is good';
if(preg_match($pattern, $target_string)) {
echo "true, ";
}
For more information, look into regular expression syntax and play around with it until you're familiar with how they work.
Like Patrick Q said put names in an array example is here
becuase you are searching all three words "google,yahoo,Bing" which can not be true.
but for beginner to understand other way.
$a[0]= "google";
$a[1]= "yahoo";
$a[2]= "Bing";
$b= "Bing is Good";
strpos($b, $a[0]); // false
strpos($b, $a[1]); // false
strpos($b, $a[2]); // true as Bing found
also you can loop through to check
You can solve your issue with a regular expression:
if (preg_match('/string1|string2|string3/i', $str)){
//if one of them found
}else{
//all of them can not found
}
Related
is there a way to check if two arrays have value that is alike, note not equal but alike. something similar to what mysql has with LIKE to find words that is similar.
in topten.txt i have the following word:
WORKFORCE
in company.txt i have the following url:
Workforce-Holdings-Ltd
So basically i would like to search for the word WORKFORCE in company.txt and the results output should be Workforce-Holdings-Ltd.
Is it possible to do it.
i was maybe thinking preg_grep() perhaps?
Preg_grep is perfect for this. As long as you are searching array values you can just do something like the following:
<?php
$companies = [
'acme inc',
'wORkForce-holdings-ltd'
];
$search = 'WORKFORCE';
var_dump(preg_grep('/' . preg_quote($search, '/') . '/i', $companies));
Here's an ideone example.
You need not use preg match. Instead you can use strpos like:
$result=array();
$a=array('WORKFORCE');
$b=array('Workforce-Holdings-Ltd');
foreach($a as $array1){
foreach($b as $array2){
if (strpos(strtolower($array2),strtolower($array1)) !== false) {
$result[] = 'true';
}
else {
$result[] = 'false';
}
}
}
This will give you an array with true if match is ok or false is match is not ok for all array elements
I have an array called 'words' storing many words.
For example:
I have 'systematic', 'سلام','gear','synthesis','mysterious', etc.
NB: we have utf8 words too.
How to query efficiently to see which words include letters 's','m','e' (all of them) ?
The output would be:
systematic,mysterious
I have no idea how to do such a thing in PHP. It should be efficient because our server would suffer otherwise.e.
Use a regular expression to split each string into an array of characters, and use array_intersect() to find out if all the characters in your search array is present in the split array:
header('Content-Type: text/plain; charset=utf8');
$words = array('systematic', 'سلام','gear','synthesis','mysterious');
$search = array('s','m','e');
foreach ($words as $word) {
$char_array = utf8_str_split($word);
$contains = array_intersect($search, $char_array) == $search;
echo sprintf('%s : %s', $word, (($contains) ? 'True' : 'False'). PHP_EOL);
}
function utf8_str_split($str) {
return preg_split('/(?!^)(?=.)/u', $str);
}
Output:
systematic : True
سلام : False
gear : False
synthesis : False
mysterious : True
Demo.
UPDATE: Or, alternatively, you could use array_filter() with preg_match():
$array = array_filter($words, function($item) {
return preg_match('~(?=[^s]*s)(?=[^m]*m)(?=[^e]*e)~u', $item);
});
Output:
Array
(
[0] => systematic
[4] => mysterious
)
Demo.
This worked to me:
$words = array('systematic', 'سلام','gear','synthesis','mysterious');
$letters=array('s','m', 'e');
foreach ($words as $w) {
//print "lets check word $w<br>";
$n=0;
foreach ($letters as $l) {
if (strpos($w, $l)!==false) $n++;
}
if ($n>=3) print "$w<br>";
}
It returns
systematic
mysterious
Explanation
It uses nested foreach: one for the words and the other one for the letters to be matched.
In case any letter is matched, the counter is incremented.
Once the letters loop is over, it checks how many matches were there and prints the word in case it is 3.
Something like this:
$words = array('systematic', 'سلام','gear','synthesis','mysterious');
$result=array();
foreach($words as $word){
if(strpos($word, 's') !== false &&
strpos($word, 'm') !== false &&
strpos($word, 'e') !== false){
$result[] = $word;
}
}
echo implode(',',$result); // will output 'systematic,mysterious'
Your question is wide a little bit.
What I understand from your question that's those words are saved in a database table, so you may filter the words before getting them into the array, using SQL like function.
in case you want to search for a letters in an array of words, you could loop over the array using foreach and each array value should be passed to strpos function.
http://www.php.net/function.strpos
why not use PREG_GREP
$your_array = preg_grep("/[sme]/", $array);
print_r($your_array);
WORKING DEMO
I'm trying to find a string inside a variable. The problem is that it doesn't look for a unique string.
For example, a have a variable with the following values:
$mystring = "p,pp,m,g";
Here's the code that I'm using:
<?php
$find="pp";
if(strstr($mystring, $find)==true){
echo "found";
}
?>
The problem is: when I'm looking for pp, it also returns "p" as a result.
How can I avoid this kind of error?
I'm using it to check the sizes of a item on an ecommerce website and I'm struggling to get it right.
Any ideas?!
Use strpos. Make sure you use the !== operator.
echo strpos($mystring, $find) !== false ? 'found' : 'not found';
$mystring = "p,pp,m,g";
$str = explode(",",$mystring);
$find = "/^pp$/";
foreach($str as $val){
if(preg_match($find, $val)){
echo "found => ".$val;
}else{
echo "not found";
}
}
ref: http://php.net/manual/en/function.preg-match.php
You can use transformating to array and search as array's element:
$mystring = "p,pp,m,g";
$arr = explode(',',$mystring);
if (in_array('p',$arr,true)) {echo "found";}
Or (http://php.net/manual/en/function.strstr.php: "If you only want to determine if a particular needle occurs within haystack, use the faster and less memory intensive function strpos() instead") you can write
if (strpos($mystring,'pp')!==false) {echo 'found';} else {echo 'not found';}
Just changed the values and used the same strstr function to avoid similar letters problem.
i am using following code to return only digital values from the variable, wounder how to get only character value "testing" from the variable and want to remove "on" from the string.
<?php
$valuecheck="testing on 123568";
$check1=preg_replace('/\D/', '', $valuecheck);
echo $check1;
?>
Output required:
testing
Thanks
Instead of \D, use [^a-zA-Z] (assuming that's what you mean by "character value"). Basically, put [^SOMETHING], where that "something" is a set of all the characters you want to consider valid.
I'm afraid my solution would be a little more verbose:
$result = '';
$c = explode( ' ', $valuecheck );
foreach ($c as $ci) {
if (preg_match( '[\D]', $ci ) == 1) { continue; }
$result = $ci; break;
}
echo $result;
Treating space as a delimiter, this will ignore any strings with at least one numeric and return the first "qualified" string found.
I currently have a problem. I have a code that looks like the one below and I want an operator that checks if a part of a query is present in an array. This is the code that I have:
<?php
$search = 'party hat';
$query = ucwords($search);
$string = file_get_contents('http://clubpenguincheatsnow.com/tools/newitemdatabase/items.php');
$string = explode('<br>',$string);
foreach($string as $row)
{
preg_match('/^(\D+)\s=\s(\d+)\s=\s(\D+)\s=\s(\d+)/', trim($row), $matches);
if($matches[1] == "$query")
{
echo "<a href='http://clubpenguincheatsnow.com/tools/newitemdatabase/info.php?id=$matches[2]'>";
echo $matches[1];
echo "</a><br>";
}
}
?>
What I want to do is instead of if($matches[1] == "$query") to check if both are identical, I want my code to see if a PART of $query exists in $matches[1]. How do I do this though? Please help me!
You can use strpos to test if a string is contained in another string:
if(strpos($matches[1], $query) !== false)
If you prefer it to be case insensitive, use stripos instead.
If you want to check if $query is a substring of $matches[1], you can use
strpos($matches[1], $query) !== false
(see documentation for why you must use !==).
You can use strstr to test if a string contains another string:
if(strstr($matches[1], $query)) {
// do something ...
}