What I'm trying to is search my string to see if there is any of the following arrays there
if not then we need to add .com to end of it.
$kwlines is my string and i have set it to test but this is what I get
test.comtest.com.comtest.com.com.comtest.com.com.com.comtest.com.com.com.com.comtest.com.com.com.com.com.comtest.com.com.com.com.com.com.com
foreach ($kwlines as $kw) {
$owned_urls= array('.com', '.co.uk', '.net','.org', '.gov','.gov.co.uk','.us');
foreach ($owned_urls as $url) {
if (strpos($kw, $url) !== TRUE) {
$kw .= ".com";
echo "$kw";
}
}
Could you please help me understand what I do wrong?
Thank you
try this:
foreach ($kwlines as $kw) {
$owned_urls= array('.com', '.co.uk', '.net','.org', '.gov','.gov.co.uk','.us');
foreach ($owned_urls as $url) {
$find = 0;
if (strpos($kw, $url) !== TRUE) {
$find = 1;
}
}
if($find == 1)
$kw .= ".com";
echo "$kw";
}
foreach ($owned_urls as $url) {
if (strpos($kw, $url) !== TRUE) {
$kw .= ".com";
echo "$kw";
exit();
}
}
Your code is running for each TLD in that array. Pull it out of the array...once the tld is found, it exists the foreach loop in my example above.
Related
Hi everyone i have faced a problem during my working to return a response for a api
the main problem is
$name = "تش";
$restaurants = [
"تشك تشيكن",
"بيتزا ساخنة",
"كينتاكي",
"تشيكن سبوت"
];
foreach ($restaurants as $restaurant) {
if (strpos($restaurant, $name)) {
echo "founded";
}
}
any help ?
Your first argument should be $item and then the keyword you are searching for which is $search
So your code should look like:
$item = "أهلا وسهلا بكم";
$search = "وسهلا";
if (strpos($item, $search)){
echo "founded";
}else {
echo "not founded";
}
The first argument in strpos should be the haystack, which is the string you want to search in, and the second argument is the needle, which is the string you want to search for
<?php
$item = "أهلا وسهلا بكم";
$search = "وسهلا";
// look if $search is in $item
if (strpos($item, $search)){
echo "founded";
}else {
echo "not founded";
}
https://3v4l.org/C07o8
based on this duplicate, Arabic characters are encoded using multibyte characters, so you need to use grapheme_strpos()
if (function_exists('grapheme_strpos')) {
$pos = grapheme_strpos($tweet, $keyword);
} elseif (function_exists('mb_strpos')) {
$pos = mb_strpos($tweet, $keyword);
} else {
$pos = strpos($tweet, $keyword);
}
I have found the answer finally
$name = "تش";
$restaurants = [
"تشك تشيكن",
"بيتزا ساخنة",
"كينتاكي",
"تشيكن سبوت"
];
foreach ($restaurants as $restaurant) {
if (strpos($restaurant, $name) !== false) {
echo "founded";
}
}
I am trying to get specified words out of an string.
I do not know if I forgot something but it only prints the first two values? Can someone help me with that?
Thanks for the help!
$orgData["connector"] = 'some stringy thing SMA-female N-female FME-female';
if (preg_match_all('/(N-female|SMA-female|FME-female)/',$orgData["connector"], $matches)) {
foreach ($matches as $i => $match) {
if ($match[$i] == "N-female") {
$con1 = "|Connector: N-female";
}
if ($match[$i] == "SMA-female") {
$con2 = "|Connector: SMA-female";
}
if ($match[$i] == "FME-female") {
$con3 = "|Connector: FME-female";
}
print_r($con1 . $con2 . $con3);
}
}
You may use a single variable and just append the matches once found:
$orgData = 'some stringy thing SMA-female N-female FME-female';
$con = "";
if (preg_match_all('/(?:N|SMA|FME)-female/', $orgData, $matches)) {
foreach ($matches[0] as $match) {
if ($match == "N-female") {
$con .= "|Connector: N-female";
}
if ($match == "SMA-female") {
$con .= "|Connector: SMA-female";
}
if ($match == "FME-female") {
$con .= "|Connector: FME-female";
}
}
}
echo $con; // => |Connector: SMA-female|Connector: N-female|Connector: FME-female
See the PHP demo.
Note I sharnk the pattern a bit: (?:N|SMA|FME)-female matches N, SMA or FME and then -female.
I am developing a system that searches for words that the user types in php files, using PHP without MySQL but I am having a problem. The system works really well when there is not a line break in the file. For example, if I search for the word "good" in a file that contains the text "good morning" works fine, but if I search for "good" in a file that contains the text "goodmorning" (with a line break) it won't list the file as a result. Here is my code:
index.php
<form action="busca.php" method="get">
<input type="text" name="s"><br>
<input type="submit">
</form>
busca.php
<?php
$pesq = (isset($_GET['s'])) ? trim($_GET['s']) : '';
if (empty($pesq)) {
echo 'Type something.';
} else {
$index = "index.php";
$busca = glob("posts/content/*.php", GLOB_BRACE);
$lendo = "";
$conteudo = "";
foreach ($busca as $item) {
if ($item !== $index) {
$abrir = fopen($item, "r");
while (!feof($abrir)) {
$lendo = fgets($abrir);
$conteudo .= $lendo;
$lendo .= strip_tags($lendo);
}
if (stristr($lendo, $pesq) == true) {
$dados = str_replace(".php", "", $item);
$dados = basename($dados);
$result[] = "$dados";
unset($dados);
}
fclose($abrir);
}
}
if (isset($result) && count($result) > 0) {
$result = array_unique($result);
echo '<ul>';
foreach ($result as $link) {
echo "<li>$link</li>";
}
echo '</ul>';
} else {
echo 'No results';
}
}
?>
Your usage of stristr is incorrect.
Compare it with false like this:
if (stristr($lendo, $pesq) !== false) {
If a string is located — the function returns the substring. Which can be casted as boolean true or false, you never know. If it doesn't find it — it returns false — the only correct value you should compare it to.
Even better to use strpos for this.
My variant:
foreach ($busca as $item) {
if ($item !== $index) {
$lendo = file_get_contents($item);
$lendo = strip_tags($lendo);
if (strpos($lendo, $pesq) !== false) {
$dados = str_replace(".php", "", basename($item));
$result[] = "$dados";
}
}
}
To fix the linebreaks - try to get rid of them
Like this:
$lendo = file_get_contents($item);
$lendo = strip_tags($lendo);
$lendo = str_replace(["\r","\n"], ' ', $lendo);
I'm working on the following but have become stumped as to how to get this to output.
I have the following which scans the directory contents, then gets the info and saves it as an array:
//SCAN THE DIRECTORY
$directories = scandir($dir);
$directinfo = array();
foreach($directories as $directory){
if ($directory === '.' or $directory === '..') continue;
if(!stat($dir.'/'.$directory)){
} else {
$filestat = stat($dir.'/'.$directory);
$directinfo[] = array(
'name' => $directory,
'modtime' => $filestat['mtime'],
'size' => $filestat['size']
);
}
}
When trying to output it however, I'm just getting single letters with a lot of breaks. Im obviously missing something here with the output loop.
foreach($directinfo as $dirInfo){
foreach($dirInfo as $drInfo){
for ($x=0; $x<=2; $x++) {
<span>"".$drInfo[$x]."<br/></span>";
}
}
}
Help is greatly appreciated. :)
You have already did everything just remove your for loop.
and try to do the following-
foreach($directinfo as $dirInfo){
foreach($dirInfo as $key=>$drInfo){
echo "<span>".$key."=>".$drInfo."<br/></span>";
}
}
I think your dealing with a 2d array, but treating it like a 3d array.
what does
foreach($directinfo as $dirInfo){
foreach($dirInfo as $drInfo){
var_dump($drInfo);
}
}
give you?
You're building a single array, dirInfo.
Php foreach takes the array first;
foreach($dirInfo as $info) {
echo "<span>" . $info['name'] . "</span>";
}
Try this function. It will return you list of all files with path.
// to list the directory structure with all sub folders and files
function getFilesList($dir)
{
$result = array();
$root = scandir($dir);
foreach($root as $value) {
if($value === '.' || $value === '..') {
continue;
}
if(is_file("$dir$value")) {
$result[] = "$dir$value";
continue;
}
if(is_dir("$dir$value")) {
$result[] = "$dir$value/";
}
foreach(getFilesList("$dir$value/") as $value)
{
$result[] = $value;
}
}
return $result;
}
I have a word:
$word = "samsung";
I have array:
$myarray = Array(
[0] => "Samsung=tv"
[1] => "Apple=mobile"
[2] => "Nokia=mobile"
[3] => "LG=tv"
I need now something like this to find a partial match:
if($word in $myarray){ echo "YES"; } // samsung found in Samsung=tv
Thank you for help.
You want in_array http://us3.php.net/manual/en/function.in-array.php.
if(in_array($word,$myarray){ echo 'yes'; }
Are you looking for something like this?
<?php
$myarray = array("Samsung=tv", "Apple=mobile", "Nokia=mobile", "LG=tv");
function findSimilarWordInArray($word, $array) {
if (empty($array) && !is_array($array) return false;
foreach ($array as $key=>$value) {
if (strpos( strtolower($value), strtolower($word)) !== false) {
return true;
} // if
} // foreach
}
// use case
if ( findSimilarWordInArray('samsung',$myarray) ) {
echo "I've found it!";
}
?>
It allows you to look for a similar word in array values.
If you are looking for partial matches, you can use strpos() with a foreach loop to iterate through the array.
foreach ($myarray as $key => $value) {
if (strpos($value, $word) !== FALSE) {
echo "Yes";
}
}
There is no built in function to do a partial match, but you can easily create your own:
function in_array_partial($needle, $haystack){
$result = false;
$needle = strtolower($needle);
foreach($haystack as $elem){
if(strpos(strtolower($elem), $needle) !== false){
$result = true;
break;
}
}
return $result;
}
Usage:
if(in_array_partial('samsung', $myarray)){
echo 'yes';
}
You can try something like this:
function my_in_array($word, $array){
forach($array as $value){
if(strpos($word, $value) !== false){
return true;
}
}
return false;
}