PHP search for keyword match in url [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Hi ,
I have a list of keywords which I need to search on the url to see if there's any match.
$keyword ='keyword1|keyword2|keyword3';
$url could be
$url = "www.keyword1castle.com";
if there is a match just echo something
How could I do this?

http://php.net/stripos
$found = false;
foreach (explode('|', $keyword) AS $val) {
$found = stripos($url, $val);
if ($found !== false) {
break;
}
}
if ($found !== false) {
echo "string found";
}

You can do something like:
explode your keyword string with | as the delimiter
loop through the array
check if the URL contains the string using strpos() (or stripos() if you want a case-insensitive check)
if it does, break out of the loop
if it doesn't, repeat the process
Code:
$keyword ='keyword1|keyword2|keyword3';
$url = "www.keyword1castle.com";
$parts = explode('|', $keyword);
foreach ($parts as $part) {
if(strpos($url, $part) !== FALSE) {
echo "keyword found in URL\n";
break;
}
else {
echo "keyword not found\n";
}
}
Documentation: explode(), strpos()
Demo!

Try this
$keyword ='keyword1';
$url = "www.keyword1castle.com";
if (strpos($url,$keyword) !== false) {
echo 'true';
}

You can use strpos : PHP.net
if (false !== strpos($url, 'keyword1')) {
echo "keyword1 found";
}

Related

Check if string exist in a set of array values PHP [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 17 days ago.
Improve this question
This has been pulled from the results of an sqli query
$colourCheckedArr = $colour_handler->pullCheckedcolour($colour_id);
foreach ($colourCheckedArr as $colourCheckedArrs) {
$list_ = $colourCheckedArrs;
$array = json_encode($list_);
echo $array . "\n";
}
I am new to this and its most likely the wrong syntax so how do I put it in the correct array syntax
Output
{"colours":"red"} {"colours":"blue"} {"colours":"green"};
From Barmar's answer
$needle = "red";
if (in_array($needle, array_column($array, "colours"))) {
echo "Match found";
return true;
} else {
echo "No match found";
return false;
}
I seem to always get
"No Match found"
Please help
Use array_column() to get an array of all the colours values, then search that.
$array = [["colours" => "red"], ["colours" => "blue"], ["colours" => "green"]];
$needle = "red";
if (in_array($needle, array_column($array, "colours"))) {
echo "Match found";
return true;
} else {
echo "No match found";
return false;
}

How to save array in RAM and make a search for an entry [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I'm trying to generate a number and save it in an array in RAM and then search for a match.
This is what my code should do:
-Generate a number
-Save it in a variable (array)
-Open a txt file and find a match from that array against the list of numbers in list.txt
-if found save it to Bingo.txt
<?php
function Fun()
{
$list = fopen("list.txt", "r");
$result = "BINGOOOOOO.txt";
//$command = "generatenumber.exe";
//$generatednumber = shell_exec($command);
$generatednumber = "1";
$matches = array();
if ($list) {
while ($getLine = fgets($list)) {
$getLine = rtrim($getLine); // remove newline
$source = $generatednumber;
var_dump($source);
if ($source) {
while ($buffer = fgets($source)) {
$buffer = rtrim($buffer);
if (in_array($getLine, explode(' ', $buffer)))
$matches[] = $buffer;
}
unset($source);
}
}
}
//show results:
print_r($matches);
$SaveResult = file_put_contents($matches, $result);
}
The issue now is that with number 1, bingo.txt file not created.
All sorts of issues in your code
An fgets() loop inside and fgets() loop will loose rows from your input
Move some of the code outside the loop, it sits better there
Get the function to return the results to you reather than outputting things itself.
function Fun($generatednumber)
{
$list = fopen("list.txt", "r");
//$command = "generatenumber.exe";
//$generatednumber = shell_exec($command);
$matches = array();
if ($list) {
while ($getLine = fgets($list)) {
$getLine = rtrim($getLine); // remove newline
if (in_array($generatednumber, explode(' ', $getLine))) {
$matches[] = $getLine;
}
}
}
return $matches;
}
$in = 1;
$matches = Fun($in);
$result = "BINGOOOOOO.txt";
$SaveResult = file_put_contents($result, $matches);

PHP function to check if a string is all lowercase [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to create a function that returns TRUE if a string is all lowercase without using any built-in PHP functions. How can I do that?
This is what I was working with, but using ctype_lower.
$string = "string";
if (ctype_lower($string)) {
echo $string . ' is all lowercase letters.';
} else {
echo $string . ' is not all lowercase letters.';
}
Well, if you just want a simple-purpose check, you could just compare the original string with an strtolowerred string. Of course, it won't be foolproof. Like this:
function check_lowercase_string($string) {
return ($string === strtolower($string));
}
$string = 'Hi! I am a string';
var_dump(check_lowercase_string($string)); // false
var_dump(check_lowercase_string('test')); // true
A painful attempt of no functions:
function check_lowercase_string($string) {
$chars = '';
// map all small characters
for($alpha = 'a'; $alpha != 'aa'; $alpha++) { $small[] = $alpha; }
$l = 0; // not strlen() :p
while (#$string[$l] != '') {
$l++;
}
for($i = 0; $i < $l; $i++) { // for each string input piece
foreach($small as $letter) { // for each mapped letter
if($string[$i] == $letter) {
$chars .= $letter; // simple filter
}
}
}
// if they are still equal in the end then true, if they are not, false
return ($chars === $string);
}
var_dump(check_lowercase_string('teSt')); // false
You can either use ctype_lower, which is inbuilt, so you don't want that :c.
But you can use the answer from here. And put together a loop which checks for the first character. If uppercase then return true, and if not, then remove the first character of string and continue like that through the entire string.

How to find word in string of words and numbers [duplicate]

This question already has answers here:
How do I check if a string contains a specific word?
(36 answers)
Closed 8 years ago.
This is example:
$haystack = "451516178jjgsjkjjssbbziznbdkbnjv.bvkljk_isikjsksjkthisjkhkjhkjh4364765467";
$needle = "this";
I need true or false.
How to do that?
I think with preg_match but I dont know how.
You don't need regex to do this. As already mentioned in comments use strpos():
if(strpos($heystack, $needle) !== false) {
// contains
}
This is how you do it with regex:
$needle = "/this/";
echo preg_match($needle, $haystack);
This will return 1 one if it matches else 0
Try this:
$haystack = "451516178jjgsjkjjssbbziznbdkbnjv.bvkljk_isikjsksjkthisjkhkjhkjh4364765467";
$needle = "this";
$string = strpos($haystack, $needle);
if($string === false)
echo "false";
else
echo "true";
You can try strpos functionality.
Example:
$title = "this is a string123";
if ((strpos($title,'is')=== false) {
echo 'false';
else
echo 'true';
}
reference : http://php.net/manual/en/function.strpos.php

Only display array values that don't contain [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
foreach ($prefs as $who => $pref) {
if($who != 'public'){
$team_users .=$who.',';
}
}
echo $team_users;
I'd like to only display data from the array that does not contain the text 'public' in some cases it could be publicXX or public1234, etc.
You can use array_filter() with a callback to achieve this:
$result = array_filter($prefs, function($item) {
return (strpos($item, 'public') === FALSE);
});
echo implode(',', $result);
http://us3.php.net/strpos
if (strpos($who, 'public') === false) {
//some code
} else {
//some code
}
I'd like to only display data from the array that does not contain the text 'public' in some cases it could be publicXX or public1234, etc.
Change:
if($who != 'public'){...}
to this:
if(strpos($who, 'public') !== false){...}
foreach ($prefs as $who => $pref) {
if(strpos($who, 'public') !== false){
$team_users .=$who.',';
}
}
echo $team_users;
You can try something like this:
foreach ($prefs as $who => $pref) {
if(!preg_match("/public/i", $who)){
$team_users .= $who.',';
}
}
<?php
$a = array('public1', 'asdqwe', 'as33publics', 'helloworld');
$text = implode(', ', array_filter($a, function($v) {
return strstr($v, 'public') === false;
}));
echo $text;
I would do it with regex.
$pattern = '/public/';
if(preg_match($pattern,$who)){
//skip to the next entry
}

Categories