Questions tagged [preg-grep]

prege_grep is a PHP function to filter array entries that match a regular expression.

prege_grep() is a function to filter array entries that match a -compatible regular expression ().

array preg_grep ( string $pattern , array $input [, int $flags = 0 ] )

Example

$arr = ["a", 1, "b", 2];
$newArr = preg_grep("/[a-z]/", $array);
print_r($newArr);

Result

Array ( 
    [0] => a 
    [2] => b 
)
40 questions
5
votes
3 answers

How can I iterate through multiple strings with multiple array values, in PHP?

* Updated question from revo's answer Here is the working script with a better set of example strings to show my intent- $strings[] = 'seventy five yards out'; $strings[] = 'sixty yards out'; $strings[] = 'one hundred fifty yards out'; $inputString…
Ryan D
  • 677
  • 6
  • 24
4
votes
1 answer

search php multidimensional associative array using key value pair with sql like '%LIKE%' construct

I have this PHP function that works well at searching a multidimensional associative array using a key value pair. I would now like to extend it to search for an array where the key value pair has an SQL-like construct similar to this: name =…
jessiPP
  • 366
  • 1
  • 5
  • 18
3
votes
2 answers

PHP preg_grep error?

PHP preg_grep not working? I'm PHP beginner, and English communication also. The execution result of this program is indicated by "ArrayArray"...
ayama
  • 31
  • 2
2
votes
3 answers

How to check if URL contains certain words?

I want to show custom content if current URL contain certain words. So far, I'm able to achieve this if the URL contains just the word, 'cart', using the code below. However, I want to be able to check for other words like 'blog', 'event' and…
cnario
  • 41
  • 4
2
votes
2 answers

Extracting numbers from string - Why do I get two arrays when using a capture group?

I am trying to extract numbers from a mix string. GA Name b
Dil Dilshan
  • 327
  • 1
  • 2
  • 16
1
vote
1 answer

How can I Extract values with information from big text file using php

I'm trying to get some data from text file, those data are repeated every random line I'm trying to get this from text file, and there are alot of 2/6 blocks. i need to get them all, using aloop. Example data I need to get it. TRAFFIC MEASUREMENT…
1
vote
3 answers

Regex for getting strings that contains only the words from the pattern list?

Consider the following array elements 1.benclinton 2.clintonharry 3.harryben 4.benwill 5.jasonsmith 6.smithclinton Assume the pattern list is ben,harry,clinton, then the result I should get is 1.benclinton 2.clintonharry …
1
vote
4 answers

how to exclude a certain folder when using scandir in php

I am using scandir to list all the files in a directory. But there should be an exception for ./, ../ and tmp folder. I already have this to exclude the dot and double dot: $files = preg_grep('/^([^.])/', scandir($dir)); How can i add tmp folder to…
Jack Maessen
  • 1,610
  • 3
  • 15
  • 34
1
vote
4 answers

How to build an indexed array of .html filenames that exist in a directory?

I am using this php code to get all html-files in the given directory: $dirpath = "....."; $filelist = preg_grep('~\.(html)$~', scandir($dirpath)); Now I want to get a specific element in the array, for example: echo $filelist[0]; This fails. In…
Ccenter
  • 87
  • 8
1
vote
1 answer

PHP preg_grep multi files

how can I exclude more files like branding.php? For example branding.php, about.php, contact.php. $pages = preg_grep('/branding\.php/', glob("*.php"), PREG_GREP_INVERT); Thx.
Ruso
  • 13
  • 2
1
vote
2 answers

How to find words with only/more specified letters using preg_grep in array?

I have sample function for searching words from array with specified letters: public static function getWords($words, $specifed_letters) { $pattern = is_array($specifed_letters) ? '/[' . implode("", $specifed_letters) . ']/u' : '/['…
Andreas Hunter
  • 2,578
  • 23
  • 58
1
vote
1 answer

PHP - How to search an associative array by matching the key against a regexp

I am currently working on a small script to convert data coming from an external source. Depending on the content I need to map this data to something that makes sense to my application. A sample input could be: $input = 'We need to buy paper…
Severin
  • 7,778
  • 10
  • 62
  • 106
1
vote
2 answers

PHP clear up Array preg_grep

I have currently a problem with arrays in php, i requested all
  • tags from an html site with: $dochtml = new DOMDocument(); $dochtml -> loadHTMLFile("my.html"); /*get all li’s*/ $lis = $dochtml -> getElementsByTagName('li'); My Html…
  • Tom Junaik
    • 13
    • 2
    1
    vote
    1 answer

    Preg grep array in pattern

    I have a problem with pattern in preg_grep. I want to pull line with specific number, source files looks like this : .... 4221 Výstavba inženýrských sítí pro kapaliny a plyny 42211 Výstavba inženýrských sítí pro kapaliny 42212 Výstavba…
    tender
    • 11
    • 1
    1
    vote
    1 answer

    filtering ids from checkboxnames

    I have some checkboxes that can be checked. They look like Now I am checking the formdata and I filter the ids in the order-array this way, before i look them up in the database. $orderids =…
    Bernhard
    • 1,745
    • 9
    • 18
    1
    2 3