Questions tagged [preg-match-all]

A PHP function that performs a global regular expression match using PCRE patterns.

A PHP function that performs a global regular expression match using PCRE patterns.

int preg_match_all ( 
     string $pattern , 
     string $subject 
     [, array &$matches 
     [, int $flags = PREG_PATTERN_ORDER 
     [, int $offset = 0 ]]] 
)

preg_match_all() in the PHP Manual

1934 questions
128
votes
1 answer

"Unknown modifier 'g' in..." when using preg_match in PHP?

This is the regex I'm trying to use: /^(\w|\.|-)+?@(\w|-)+?\.\w{2,4}($|\.\w{2,4})$/gim I found it on this site, and it works great when I try it out there. But as soon as I place it in my code, I get this message: Warning: preg_match()…
Nike
  • 1,479
  • 3
  • 10
  • 9
52
votes
3 answers

PHP's preg_match() and preg_match_all() functions

What do the preg_match() and preg_match_all() functions do and how can I use them?
sikas
  • 5,435
  • 28
  • 75
  • 120
34
votes
6 answers

How to extract a string from double quotes?

I have a string: This is a text, "Your Balance left $0.10", End 0 How can I extract the string in between the double quotes and have only the text (without the double quotes): Your Balance left $0.10 I have tried preg_match_all() but with no…
conandor
  • 3,637
  • 6
  • 29
  • 36
31
votes
3 answers

Getting the first image in string with php

I'm trying to get the first image from each of my posts. This code below works great if I only have one image. But if I have more then one it gives me an image but not always the first. I really only want the first image. A lot of times the second…
MHowey
  • 681
  • 2
  • 6
  • 19
20
votes
8 answers

How to get all captures of subgroup matches with preg_match_all()?

Update/Note: I think what I'm probably looking for is to get the captures of a group in PHP. Referenced: PCRE regular expressions using named pattern subroutines. (Read carefully:) I have a string that contains a variable number of segments…
hakre
  • 193,403
  • 52
  • 435
  • 836
15
votes
2 answers

Get all matches from string that start with and end, using php

How would I use PHP to extract everything in between [startstring] and [endstring] from this string: [startstring]hello = guys[endstring] hello guys [startstring]jerk = you[endstring] welcome to the universe I'd like to find all the matches of…
user1935281
14
votes
2 answers

PHP: How do I get the string indexes of a preg_match_all?

let's say I have two regexp's, /eat (apple|pear)/ /I like/ and text "I like to eat apples on a rainy day, but on sunny days, I like to eat pears." What I want is to get the following indexes with preg_match: match: 0,5 (I like) match: 10,19 (eat…
atp
  • 30,132
  • 47
  • 125
  • 187
12
votes
6 answers

Get repeated matches with preg_match_all()

I'm trying to get all substrings matched with a multiplier: $list = '1,2,3,4'; preg_match_all('|\d+(,\d+)*|', $list, $matches); print_r($matches); This example returns, as expected, the last match in [1]: Array ( [0] => Array ( …
BenMorel
  • 34,448
  • 50
  • 182
  • 322
12
votes
7 answers

PHP Regex Check if two strings share two common characters

I'm just getting to know regular expressions, but after doing quite a bit of reading (and learning quite a lot), I still have not been able to figure out a good solution to this problem. Let me be clear, I understand that this particular problem…
Paul Hazen
  • 2,361
  • 2
  • 16
  • 21
11
votes
3 answers

preg_match_all and preg_replace in Ruby

I am transitioning from php to ruby and I am trying to figure the cognate of the php commands preg_match_all and preg_replace in ruby. Thank you so much!
Spencer
  • 21,348
  • 34
  • 85
  • 121
11
votes
4 answers

How to get text in array between all tag from HTML?

I want to fetch text in array between all tag from HTML, I have tried with this code but it returns only one occurrence : preg_match('/(.+?)<\/span>/is', $row['tbl_highlighted_icon_content'], $matches); echo $matches[1]; My…
Wiram Rathod
  • 1,895
  • 1
  • 19
  • 41
10
votes
3 answers

Regex - Ignore some parts of string in match

Here's my string: address='St Marks Church',notes='The North East\'s premier...' The regex I'm using to grab the various parts using match_all is '/(address|notes)='(.+?)'/i' The results are: address => St Marks Church notes => The North…
Paul Phillips
  • 1,480
  • 1
  • 15
  • 23
10
votes
3 answers

getting emails out of string - regex syntax + preg_match_all

I'm trying to get emails out of a string: $string = "bla bla pickachu@domain.com MIME-Version: balbasur@domain.com bla bla bla"; $matches = array(); $pattern =…
homerun
  • 19,837
  • 15
  • 45
  • 70
9
votes
3 answers

Is there a way to pass another parameter in the preg_replace_callback callback function?

mmmh guys, i really hope my english is good enaught to explain what i need. Lets take this example (that is just an example!) of code: class Something(){ public function Lower($string){ return strtolower($string); } } class Foo{ …
Strae
  • 18,807
  • 29
  • 92
  • 131
9
votes
4 answers

Why is preg_match_all returning two matches?

I am trying to identify if a string has any words between double quotes using preg_match_all, however it's duplicating results and the first result has two sets of double quotes either side, where as the string being searched only has the one…
Styphon
  • 10,304
  • 9
  • 52
  • 86
1
2 3
99 100