Sandeep Bansal
Sandeep Bansal

Reputation: 6394

preg_match_all regex value in between two tags

I'm trying to build out a preg_match_all so I can use the array, but the problem is that I can't refine my regex to work.

An example input would be:

#|First Name|#
This text shouldn't be caught
#|Second Value|#

First Name and Second Value should both be in the array.

Here's what I've tried:

preg_match_all('/\#\|(.*?)\|\#\]/',$html, $out);

and

preg_match_all ("/^#\|*\|#*$/i", $html, $out);

Upvotes: 1

Views: 298

Answers (4)

hackattack
hackattack

Reputation: 1087

a couple of things, you need to add the '.' character in between your bars. This character means match any character, without it you are saying match zero or more '|' characters. you also need to add the m modifier for multiline matching.

/^#\|.*\|#$/im

Upvotes: 0

LSerni
LSerni

Reputation: 57388

Try with:

preg_match_all('/#\|(.*?)\|#/', $html, $out);

Escaping # should not be needed. To specify no # between #'s, you might also use

preg_match_all('/#\|([^#]*)\|#/', $html, $out);

Upvotes: 0

Joost
Joost

Reputation: 10413

The first is about right, it only contains \] near the end which breaks what you want. Using

/\#\|(.*?)\|\#/

does give me correct results, with the matches in $out[1].

Upvotes: 1

PoulsQ
PoulsQ

Reputation: 2016

Try this regexp :

/\#\|(.*?)\|\#/

Upvotes: 1

Related Questions