0

I am trying to loop through a string that contains html from a scraped webpage. First I look to return all links that contain the word "result" and then I would like to organize all the links that contain one of four cases, "base", "second", "third" or "latest" and create a fluid array.

Below is what I have come up with but it returns "Warning: strpos(): needle is not a string or an integer". I cannot seem to get the array cases to work.

Any help would be greatly appreciated. Thank you

    $key = "results";
    $reportKey = array("base", "second", "third","latest");
    $keyArray = array();
    foreach($html->find('a') as $element){
        if (strpos($element->href, $key) !== false){
            if (strpos($element->href, $reportKey) !== false){
                $keyArray[] = $element->href;
            }
        }
    }
    echo "<pre>" . print_r($keyArray) . "</pre> ";
1
  • 1
    I guess you could try to make sure that an <a> does have an href attribute before trying to access it. Commented Jun 28, 2018 at 12:26

3 Answers 3

4

You can't use an array as a needle in strpos. Change second if to:

if (str_replace($reportKey, "", $element->href) === $element->href) {
    $keyArray[] = $element->href;
}
Sign up to request clarification or add additional context in comments.

Comments

1

strpos() does not allow more than one needle, you can do this:

$key = "results";
$reportKey = array("base", "second", "third","latest");
$keyArray = array();

foreach($html->find('a') as $element)
{
    if (strpos($element->href, $key) !== false){
        if (
            strpos($element->href, $reportKey[0]) !== false
            || strpos($element->href, $reportKey[1]) !== false
            || strpos($element->href, $reportKey[2]) !== false
            || strpos($element->href, $reportKey[3]) !== false
         ){
             $keyArray[] = $element->href;
         }
     }
 }

 echo "<pre>" . print_r($keyArray) . "</pre> ";

You could also do your own function, this is only an example:

function multi_strpos($string, $check, $getResults = false)
{
$result = array();
  $check = (array) $check;

  foreach ($check as $s)
  {
    $pos = strpos($string, $s);

    if ($pos !== false)
    {
      if ($getResults)
      {
        $result[$s] = $pos;
      }
      else
      {
        return $pos;          
      }
    }
  }

  return empty($result) ? false : $result;
}

1 Comment

For my purposes, your first suggestion worked. Thanks!
0

A solution using array_map() and in_array():

$key = 'results';
$reportKey = ['base', 'second', 'third', 'latest'];
$keyArray = [];

foreach($html->find('a') as $element) {
    if (false !== strpos($element->href, $key)) {

        // I changed the condition here
        $pos = array_map(fn($k) => strpos($element->href, $k) !== false, $reportKey);

        if (in_array(true, $pos)){
            $keyArray[] = $element->href;
        }
    }
}

$pos will be an array containing booleans based on matches between $element->href and $reportKey items.
Then we check with in_array() if it matched at least one time.

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.