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
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,852
    • 11
    • 19
    1
    vote
    2 answers

    PHP text to Array Regex

    I need the text delimited by "~ (Number from 0 to 13)" and ending at "~ end" each position of the array should have the text that is between the braces. Does anybody have an idea ? TEXT: (The original has a lake text and maybe html) ~0 …
    victorelec14
    • 178
    • 12
    1
    vote
    1 answer

    Grep for a specific pattern in a file and stop at the first match

    I have a bunch of Nginx vhost files like this: # This file was autogenerated by Puppet [ wordpress::vhost ] # Any manual edit to this file will be automatically removed # ©2016, DevOps team server { listen 443 ssl; root /var/www/as888; …
    MacUsers
    • 2,091
    • 3
    • 35
    • 56
    1
    vote
    0 answers

    Makes preg_grep keep and not reorder the arrays keys after each execution

    I'm trying to clean my email database that was imported from an excel table. So there's plenty of bad characters, and even two or three emails in the same cell. I dont want to use a direct database solution as was posted here (T-SQL: checking for…
    Sam
    • 11
    • 2
    1
    vote
    3 answers

    Regular expression to match IP addresses

    I have file.txt which contains this data: livebox.home (192.168.1.1) set-top-box41.home (192.168.1.10) pc38.home (192.168.1.11) pc43.home (192.168.1.12) pc39.home (192.168.1.15) I want to extract the IP of pc39.home. I have used this regular…
    Ktari zied
    • 157
    • 1
    • 2
    • 13
    1
    vote
    1 answer

    permutation using regular expression in php

    Is there any way to get permutation of all words in a string using only regular expression in php. For example: for input like "How to remove pain in human knee" I want output as: "How To", "How","pain knee","remove knee","knee pain","in…
    Ankit Agarwal
    • 1,350
    • 10
    • 15
    1
    vote
    2 answers

    Preg-match filenames with hypens

    How do I check filenames with preg_match in PHP? Filename = 2.pdf It should also match if another file is called 2-2.pdf or 2-3.pdf. It's the Id before the hypen. It dont need to check the .pdf file extension. $id = 2; $list =…
    Stefan Frederiksen
    • 135
    • 2
    • 3
    • 15
    0
    votes
    2 answers

    filter and count filtered array

    I'm trying to filter a multidimensional array and return the number of entries found for a given category. EDIT I'm trying to avoid any use of foreach. Right now looking into using array_intersect but getting array to string conversion error. I'm…
    user10844414
    0
    votes
    1 answer

    preg_grep does not return the right results

    The following code does not capture 45.00 as a result: $array = array(50,45.00,34,56,6.67); $fl_array = preg_grep("/^(\d+)?\.(\d)+$/", $array); Any suggestion?
    Konstantinos
    • 19
    • 1
    • 1
    • 3
    0
    votes
    1 answer

    preg_grep(): Compilation failed: nothing to repeat at offset 0

    After updating my PHP from 5.3 to 7.1.9, I keep getting this error on my get_browser_language function. I tried to debug it using several method but no luck, the variables etc all are correct. Code; function get_browser_language( ) { global…
    Pelin
    • 467
    • 4
    • 17
    0
    votes
    2 answers

    PHP array containing 0/

    hi I have array of elements containing pattern like $arr = array ('0/' ,'0/12/3','1/2') i need to have array of "0/" elements i've tried to use command arr_with_zero_slash = preg_grep('@$[0-9]/$@',$arr) but function works only witch pattern like…
    Adam Krawczyk
    • 71
    • 1
    • 5
    0
    votes
    1 answer

    preg_grep outputting partial match

    So I'm currently using preg_grep to find lines containing string, but if a line contains special chars such as " - . @ " I can simply type 1 letter that is within that line and it will output as a match.. example of line example@users.com search…
    user3255841
    • 113
    • 1
    • 2
    • 8
    0
    votes
    0 answers

    Find element of an array those have only specific character set using preg_grep() in php

    I need to find only those elements of an array that have specific set of letters and any character before or after of finds word using PHP preg_grep() from array. I have an array like following $findgroup = array("sten", "netff", "enet",…