2

This one is bit tricky , I have an array and I need to keep only certain value string inside of it

$getpositions = file("index.php");
$searchpoz = array('NEED1', 'NEED2', 'WANT THIS ALSO','ANDTHIS');

function strposa($haystack, $needles=array(), $offset=0) {
        $chr = array();
        foreach($needles as $needle) {
                $res = strpos($haystack, $needle, $offset);
                if ($res !== false) $chr[$needle] = $res;
        }
        if(empty($chr)) return false;
        return min($chr);
}//http://stackoverflow.com/a/9220624/594423


foreach($getpositions as $key => $clearlines) {
    if(strposa($clearlines, $searchpoz) == false)
        unset($getpositions[$key]);
}
$positionsorder = array_values($getpositions);
print_r($positionsorder);

Array
(
    [0] =>      i dont need this NEED1 i dont need this

    [1] =>      i dont need this NEED2 i dont need this

    [2] =>      i dont need this WANT THIS ALSO i dont need this

    [3] =>      i dont need this ANDTHIS i dont need this

)

so desired output should be

Array
(
    [0] =>NEED1

    [1] =>NEED2

    [2] =>WANT THIS ALSO

    [3] =>ANDTHIS

)

notice that I need to remove everything before and after the desired value

any help is appreciated , thank you!

4
  • Please, post your original array and desired output. For now I see your issue as - filter needle array, excluding those items, which were not found in original array Commented Dec 25, 2013 at 12:50
  • the original array is a php file which with file() puts all lines into and array ,as you can see above I kept only the lines that contain the specific strings but I dont need the complete line , I need just the string Commented Dec 25, 2013 at 12:56
  • So - again - if you need only string, then your issue is for each string - check if something from needle array is inside this string - and, if yes, return first found needle element. Am I right? Commented Dec 25, 2013 at 13:02
  • 1
    well yes , I see that I need an else after I unset the lines I dont need , and replace the value with the matching needle . easier said than done but im on it Commented Dec 25, 2013 at 13:09

2 Answers 2

1
$matches = [];
// don't really need array?
$getpositions = implode('', $getpositions);

foreach($searchpoz as $val){
    $pos = strpos($getpositions, $val);
    if($pos !== false) $matches[$val] = $pos;
}

// preserve order of occurrence.
asort($matches);
print_r(array_keys($matches));

: demo

Sign up to request clarification or add additional context in comments.

2 Comments

@Benn It doesn't need to be though - unless you are using it for something else? (in which case - simply rename the variable...) - It's just an alternative without regex.
yes , just noticed that , nice one and short , seems faster also
1

If you need only string, then your issue is for each string - check if something from needle array is inside this string - and, if yes, return first found needle element. That can be easily implemented with:

$file = [
    'i dont need this NEED1 i dont need this',
    'crap crap crap',
    'i dont need this NEED2 i dont need this',
    'garbage garbage garbage',
    'i dont need this WANT THIS ALSO i dont need this',
    'unused unused unused',
    'i dont need this ANDTHIS i dont need this',
];

$needle = ['NEED1', 'NEED2', 'WANT THIS ALSO','ANDTHIS'];
$result = [];
array_map(function($item) use ($needle, &$result)
{
   //regex creation may be done before iterating array - that will save resources
   if(preg_match('/'.join('|', array_map('preg_quote', $needle)).'/i', $item, $matches))
   {
      $result[] = $matches[0];
   }
}, $file);
//var_dump($result);

1 Comment

great one but Emissarry one seems to be faster , not 100% on it

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.