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 
)
42 questions
6
votes
6 answers

Native function to filter array by prefix

Say I have an array with the following members: car_porsche car_mercedes car_toyota motorcycle_suzuki motorcycle_honda motorcycle_motoguzzi How can I get an array with all the elements starting with car_? There was a native function for this but I…
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
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
  • 741
  • 1
  • 11
  • 29
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
  • 436
  • 1
  • 6
  • 21
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
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
  • 361
  • 1
  • 4
  • 17
1
vote
0 answers

How to Read and Display specific information from a text file from user attached file without saving into database in PHP or Javascript?

please help a brother! I'm writing a script to be able to read user's .txt file in order to search a specific keyword, say "model number" and display in the textarea of my html page. I wanted to achieve this extraction on real-time base, i.e the the…
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
1 answer

Filter multidimensional array by keywords in (ex: title or description) values (PHP)

How to filter multidimensional array with specific keyword in 'title' OR 'description' values, like the data array below: Array ( [0] => Array ( [id] => 873 [title] => Mark [description] => John Doe…
hijaumuda
  • 25
  • 4
1
vote
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
  • 31
  • 1
  • 5
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,780
  • 4
  • 19
  • 51
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
  • 107
  • 10
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
  • 4,504
  • 11
  • 65
  • 125
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
  • 8,508
  • 14
  • 68
  • 117
1
2 3