1

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

4
  • "inactive" OR "nofollow" - this. You can't do this. Commented Oct 31, 2014 at 8:47
  • You need to do if (stristr($unfinished_link, "inactive") || stristr($unfinished_link, "nofollow")) { ... } Commented Oct 31, 2014 at 8:48
  • I have removed the OR and just left the "inactive"...still getting the same result Commented Oct 31, 2014 at 8:49
  • stristr does not return TRUE, the condition should be !== FALSE Commented Oct 31, 2014 at 8:51

5 Answers 5

1

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');
Sign up to request clarification or add additional context in comments.

Comments

1

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.

2 Comments

ok @Serpes I have echoed the $link_body[$key] but now Im getting an undefined offset error.
Obviusly. If you unset it you can't echo it. Or you echo it in an else statement or you use another if statement like: if(isset($link_body[$key]))
1

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

Comments

1

Using array_filter :

function filterLink($link) {
 return  stripos($unfinished_link, 'inactive') === false &&
         stripos($unfinished_link, 'nofollow') === false
}

$unfinished_link = array_filter($unfinished_link, "filterLInk")

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.