I have a series of string templates, for example:
Confirmation Number: 1234
Other random number: 1231
Order Name: Something
What I'm trying to do is getting these numbers and names from the template and use them as variables... I know it's very simple, but how would I go about doing this?
Cheers!
EDIT: The best approach for me to do this would be entering something like:
Confirmation Number: {CONFIRMATION_NUMBER}
Other random number: {OTHER_NUMBER}
Order Name: {ORDER_NAME}
And then get whatever's in the brackets from the template.
Hi something like this might do the trick.
preg_match('/^(?P<key>.+)\:\s*(?P<value>[\w]+)$/', $line, $match);
then use $match['key']
and $match['value']
demo http://regex101.com/r/eN1rW0/2
You might need a space in the value?
preg_match('/^(?P<key>.+)\:\s*(?P<value>[\w\s]+)$/', $line, $match);
Or anything in the value?
preg_match('/^(?P<key>.+)\:\s*(?P<value>.+)$/', $line, $match);
Demo
http://regex101.com/r/eN1rW0/3
To capture the :
you just want to move that inside of the (
capture group )
like so
preg_match('/^(?P<key>.+\:)\s*(?P<value>.+)$/', $line, $match);