Find letters in the middle of the preg_grep array

Asked

Viewed 213 times

1

I’m really bad at doing this kind of research and I don’t have much idea how to do it, the way I did I can only locate the word if written in full if it’s in sequence (from start to finish).

How can I locate by letters only instead of words by integers, being located anywhere in the sub-array names?

<?php
    $find = 'as'; //para localizar o 'foo bar bas' no sub array names na quarta posição
    $directories = array (
        'names' => array(
            'hi',
            'hello pt.stackoverflow',
            'hello world',
            'foo bar bas',
        ),
        'dir' => array(
            'directory 1',
            'directory 2',
            'directory 3',
            'directory 4'
        ),
        'type' => array(
            'file',
            'folder',
            'file',
            'folder'
        )
    );
    $matches = preg_grep('/^'.$find.' (\w+)/i', $directories['names']);
    $keys = array_keys($matches);
    if(count($keys) != 0){
        echo count($keys) . ' resultados encontrados </br>';
        foreach($keys as $index) {
            echo 
                $directories['names'][$index] . ' | ' . 
                $directories['dir'][$index]  . ' | ' . 
                $directories['type'][$index] . '</br>';
        }
    }else{
        echo 'Sem resultados para a busca ' . $find . '</br>';
    }
?>

2 answers

3


About the regex you used: '/^'.$find.' (\w+)/i'. As the variable $find is "as", the end result is /^as (\w+)/i.

Bars are regex delimiters and are not part of the expression itself.

Then we have the bookmark ^, meaning "beginning of the string". Then we have the letters "as", followed by space, followed by \w+.

The shortcut \w corresponds to "letters, digits or the character _", and the quantifier + means "one or more occurrences".

That is, regex looks for strings that begin with "as", followed by a space, followed by one or more \w (letters, digits or _). And clearly the string you are searching for ('foo bar bas') does not correspond to the expression.

If you want to search for "as" anywhere in the string, the expression is /as/. In case, just do so:

$matches = preg_grep('/'.$find.'/', $directories['names']);

With that, the string 'foo bar bas' is found, since regex now searches for "as" anywhere in the strings (not only at the beginning), and does not require that there is a space and other characters.


In your code you also used the option i, that makes the regex case insensitive. I mean, if you do:

$find = 'as';
$matches = preg_grep('/'.$find.'/i', $directories['names']);

Strings containing "as", "AS", "as" or "As" will be found. If you remove the i, only strings containing "the".


And if you only want the strings ending in "as", you can use the marker $ (string end):

$matches = preg_grep('/'.$find.'$/i', $directories['names']);

0

From what I understand of your doubt, the regex would only be the search term:

$matches = preg_grep('/'.$find.'/', $directories['names']);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.