This is the code I have written so far:
foreach ($link_body as $key => $unfinished_link)
{
#further Remove non ad links
if (stristr($unfinished_link, "inactive" OR "nofollow") === TRUE)
{
unset($link_body[$key]);
}
echo "<font color='#FFFFFF' size='16'>$unfinished_link</font><br>";
}
I'm not getting any error messages but I keep getting results that look like this:
/cars/" />
/cars/">
/cars/" class="inactive">(not what I wanted)
/cars/" class="inactive">(not what I wanted)
/cars/" class="inactive">(not what I wanted)
/cars/" rel="nofollow">(not what I wanted)
/cars/?layout=gallery" rel="nofollow">(not what I wanted)
/cars/2001-ford-escort-great-condition/1235588">(IS what I wanted)
Where am I messing up here guys? Thx
If you're trying to find a string inside that, maybe you indend to use stripos instead:
foreach ($link_body as $key => $unfinished_link) {
// further Remove non ad links
if(
stripos($unfinished_link, 'inactive') !== false ||
stripos($unfinished_link, 'nofollow') !== false
) {
unset($link_body[$key]);
} else {
echo "<font color='#FFFFFF' size='16'>$unfinished_link</font><br>";
// make sure your background is not white, or else your text will not be seen, at least on the white screen
}
}
If this is an HTML markup, consider using an HTML parser instead, DOMDocument in particular, and search for that attributes:
$rel = $node->getAttribute('rel'); // or
$class = $node->getAttribute('class');
You echo the variable $unfinished_link that it's different that $link_body[$key]. OK, the values are the same before you unset $link_body[$key] but it's like you are doing:
$a=1;
$b=1;
unset($a);
echo $b;
Of course, this code will echo the number one because I have unset a variable and echo other one. Also the condition of the If is incorrect.
Do not remove array's element inside foreach statement
Remember elements to delete in foreach, delete them after foreach:
$elements_to_delete = {};
foreach ($link_body as $key => $unfinished_link)
{
if(stristr($unfinished_link, "inactive" OR "nofollow") === TRUE) {
$elements_to_delete.push($key);
}
}
// removing elements after foreach complete
foreach($key in $elements_to_delete){
$link_body[$key];
}
Using array_filter :
function filterLink($link) {
return stripos($unfinished_link, 'inactive') === false &&
stripos($unfinished_link, 'nofollow') === false
}
$unfinished_link = array_filter($unfinished_link, "filterLInk")
To my knowledge, you cannot combine parameters in the way you try here :
if(stristr($unfinished_link, "inactive" OR "nofollow") === TRUE)
Instead, you can replace with
if(stristr($unfinished_link, "nofollow") === TRUE) || stristr($unfinished_link, "inactive") === TRUE)
Related
just want to say thanks to this community for bailing me out countless times. Today I am trying to create a template override in a Joomla component to only display results that have "DE" in the title when the URL contains "/de/". I've tried a few things, but keep getting blank results. Here is what I have so far:
$keywords = " DE";
$title = JHtml::_('link', $link, $item->title); // Gets Pathway Title
if ((strpos($item, "DE") || strpos($_SERVER['REQUEST_URI'], "de")) == false) {
$item = $displayData;
} else {
$item = array_filter($displayData, function (array $item) use ($keywords) {
return array_key_exists('title', $item) && $item['title'] === $keywords;
});
}
Not sure how to get titles that contain " DE" at the end. Can anyone help me?
You need to test each strpos() result separately, not combine them with ||. You also have to use === or !== when testing the result of strpos(), because loose comparison will treat 0 == false as true.
It's also easier to understand if you use positive conditions rather than negatives.
if (strpos($_SERVER['REQUEST_URI'], "de")) !== false && strpos($item, "DE") !== false) {
$item = array_filter($displayData, function (array $item) use ($keywords) {
return array_key_exists('title', $item) && strpos($item['title'], $keywords) !== false;
});
} else {
$item = $displayData;
}
I have an array. Inside that array my Fruit_Name could either be Pear or Apple depending on the button i select in the previous page. Lets say i select Apple the if statement does not seem to work however it does echo. It echos$FruitType and it seems to get Apple which should fire up my if statement and show me "You did it!", but my if condition doesn't. What am i doing wrong?
Array
Fruit_Name=Apple
My Function
function GetField($arr, $field)
{
$result = ' ';
foreach($arr as $line)
{
if (explode('=', $line) [0] == $field)
{
$result = explode('=', $line) [1];
}
}
return $result;
}
$FruitType= GetField($array, 'Fruit_Name');
echo $FruitType;
if ($FruitType == "Apple")
{
echo "You did it!";
}
else if ($FruitType == "Pear")
{
echo "Its not Pear!";
}
When comparing strings (and really, you should do this with any data type) in php, you must use the === notation. The == is only used for numbers, and when used on strings leads to problems like the one you're facing.
So your code should be
If ( $FruitType === "Apple" )
And the Getfield function is incorrect, as mentioned and explained by Dagon.
What is wrong with this code? I've tried using array_udiff without any success.
<?php
#I want to echo values of $paths1 that do not appear (even partially) on $paths2.
$paths1 = array('one', 'two', 'three');
$paths2 = array('twenty one', 'twenty two');
foreach ($paths1 as $path1)
{
foreach ($paths2 as $path2)
{
if (stripos($path1, $path2) == False)
{
echo $path1 . "<br>";
break;
}
}
echo "<br>";
}
?>
You need to use stripos() === false, as if they match it's going to return 0 which is == to false.
You have your parameters mixed, it should be stripos($path2, $path1).
You need to check all values in $paths2 until you find one it is in. You are saying it's not in any $paths2 after the first one you don't find it in. Set a flag of $flag = true; between the foreach() loops. Instead of echoing inside the second foreach, just set $flag == false if stripos($path2, $path1) !== false. After the second loop ends, but before the first, output if $flag == false.
ie
foreach ($paths1 as $path1)
{
$flag = true;
foreach ($paths2 as $path2)
{
if (stripos($path2, $path1) !== false)
{
$flag = false;
break;
}
}
if($flag)
echo $path1;
}
Note: didn't test it, but should work.
The arguments to stripos are backwards. Instead of:
if (stripos($path1, $path2) == False)
You want:
if (stripos($path2, $path1) === false)
I want to return index of the array. I am trying the following below but its not working:
$os = array("Helo how are you","I am you");
foreach ($os as $oss) {
if(strpos($oss, 'you') !== false) {
$reference= array_search('you', $oss);
echo $reference;
}
}
I would want the reference to echo 0, 1. given that I am searching for a word 'you' and it occurs in 0, 1. Is array search for a single string? What can do to search in the whole of substring?
array_search() wouldn't work here as it can't search for partial matches. Instead, you can use the following approach: if the strpos() statement evaluates to true, simply display the index for that iteration:
foreach ($os as $reference => $oss)
{
if(strpos($oss, 'you') !== false) {
echo $reference, PHP_EOL;
}
}
Output:
0
1
Demo
Try changing your foreach loop to this:
foreach ($os as $index => $oss)
{
if(strpos($oss, 'you') !== false) {
$reference= array_search('you', $oss);
echo $index;
}
}
I need to search a string for any occurances of another string in PHP. I have some code that I've been playing with, but it doesn't seem to be working.
Here is my code:
while (list($key, $val) = each($keyword)) {
$pos = strpos($location, $val);
if($pos == false) {
echo "allow";
exit;
} else {
echo "deny";
exit;
}
}
I have tried some of the options below, but it still does not find the match. Here is what I'm searching:
I need to find:*
blah
In:
http://blah.com
Nothing seems to find it. The code works in regular sentences:
Today, the weather was very nice.
It will find any word from the sentence, but when it is all together (in a URL) it can't seem to find it.
When checking for boolean FALSE in php, you need to use the === operator. Otherwise, when a string match is found at the 0 index position of a string, your if condition will incorrectly evaluate to true. This is mentioned explicitly in a big red box in the php docs for strpos().
Also, based on a comment you left under another answer, it appears as though you need to remove the exit statement from the block that allows access.
Putting it all together, I imagine your code should look like this:
while (list($key, $val) = each($keyword)) {
$pos = strpos($location, $val);
if($pos === false) { // use === instead of ==
echo "allow";
} else {
echo "deny";
exit;
}
}
Update:
With the new information you've provided, I've rewritten the logic:
function isAllowed($location) {
$keywords = array('blah', 'another-restricted-word', 'yet-another-restricted-word');
foreach($keywords as $keyword) {
if (strpos($location, $keyword) !== FALSE) {
return false;
}
}
return true;
}
$location = 'http://blah.com/';
echo isAllowed($location) ? 'allow' : 'deny';