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!

Benn
  • 4,840
  • 8
  • 65
  • 106
  • 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_ – Alma Do Dec 25 '13 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 – Benn Dec 25 '13 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? – Alma Do Dec 25 '13 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 – Benn Dec 25 '13 at 13:09

2 Answers2

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);
Alma Do
  • 37,009
  • 9
  • 76
  • 105
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

Emissary
  • 9,954
  • 8
  • 54
  • 65
  • @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. – Emissary Dec 25 '13 at 13:35
  • yes , just noticed that , nice one and short , seems faster also – Benn Dec 25 '13 at 13:37