LargeTuna
LargeTuna

Reputation: 2824

PHP Parse out string between two percent % signs

I have a block of text and I am trying to create an array by parsing out all of the strings which are inbetween two %% signs like %string_name%...

Here is a sample of the text:

The total % commission on property mentioned above is 6% of %purchase_amount% (purchase price) = %total_commission%...

I am wanting to just get %total_commission% and %purchase_amount% but my script is messing up because of the "6%" string in the text and the "% commission" because it thinks it needs to grab these strings too because they have percent signs...

How can I modify this preg_match_all function to just grab text that has two percent signs?

Here is my preg_match_all function:

preg_match_all('/%[^%]*%/', $theLargeTextBlockVariable, $matches);

Upvotes: 0

Views: 217

Answers (1)

raina77ow
raina77ow

Reputation: 106443

One possible approach:

preg_match_all('/%[a-z_]+?%/', $theLargeTextBlockVariable, $matches);

The pattern now consumes only latin letters or underscores. And it's made greedy for a bit better performance.

Upvotes: 4

Related Questions