Reputation: 46
I'm having an issue trying to find all the variables in an html file.
The HTML file contains an email template, when the email itself is sent out, it converts variables like "$EMAIL_FIRST_NAME" into things like "John" because information is sent to the email function to replace all occurrences of "$EMAIL_FIRST_NAME" with "John". The issue I'm having is trying to help people create these email templates. I want to provide them the ability to insert test data.
They can take their email template (editing in a textarea) and by use of jquery it loads their template into a new window to preview. And I have it replacing some "stock" fields. However I'm running into the issue where it would be nice for them to be able to add test data.
And since each template is for different purposes it would be nice to have the correct fields appear. I'm looking for a way to use PHP to look through the HTML template and find the variable to compile an array (to be used to create input boxes).
Some snippets for examples:
<meta http-equiv="Content-Type" content="text/html; charset=$CHARSET">
$INTRO_ORDER_NUM_TITLE $INTRO_ORDER_NUMBER
$INTRO_DATE_TITLE $INTRO_DATE_ORDERED
<a href="$WEBSITE_ADDRESSindex.php?main_page=contact_us">$WEBSITE_ADDRESSindex.php?main_page=contact_us</a>
I'm looking for a way to give an array like this:
array('$CHARSET','$INTRO_ORDER_NUM_TITLE','$INTRO_ORDER_NUMBER','$INTRO_DATE_TITLE','$INTRO_DATE_ORDERED','$WEBSITE_ADDRESS')
Some of them I could just do an explode with a space, and then find those that start with $. However the others namely $WEBSITE_ADDRESS is a bit more challenging as the rest is not part of the variable.
All the variables are suppose to start with $ and be all capital letters.
I'm looking for a way to find the sub strings that start with $ and then through the last capital letter.
Ideas?
Upvotes: 0
Views: 83
Reputation: 6539
Try this one:-
$str = '<meta http-equiv="Content-Type" content="text/html; charset=$CHARSET">
$INTRO_ORDER_NUM_TITLE $INTRO_ORDER_NUMBER
$INTRO_DATE_TITLE $INTRO_DATE_ORDERED
<a href="$WEBSITE_ADDRESSindex.php?main_page=contact_us">$WEBSITE_ADDRESSindex.php?main_page=contact_us</a>';
$input = preg_match_all('/\$[A-Z_]+/', $str, $match);
$result = array_unique($match[0]);
echo '<pre>'; print_r($result);
Output:-
Array
(
[0] => $CHARSET
[1] => $INTRO_ORDER_NUM_TITLE
[2] => $INTRO_ORDER_NUMBER
[3] => $INTRO_DATE_TITLE
[4] => $INTRO_DATE_ORDERED
[5] => $WEBSITE_ADDRESS
)
Upvotes: 1
Reputation: 43169
In addition to npintis regex, here's an example with a callback function. I would change the $
sign to something not so complicated for PHP (e.g. _
before and after):
<?php
$tmpl = '<meta http-equiv="Content-Type" content="text/html; charset=_CHARSET_">
_INTRO_ORDER_NUM_TITLE_ _INTRO_ORDER_NUMBER_
_INTRO_DATE_TITLE_ _INTRO_DATE_ORDERED_
<a href="_WEBSITE_ADDRESS_index.php?main_page=contact_us">_WEBSITE_ADDRESS_index.php?main_page=contact_us</a>';
$allowed = array('_CHARSET_','_INTRO_ORDER_NUM_TITLE_','_INTRO_ORDER_NUMBER_','_INTRO_DATE_TITLE_','_INTRO_DATE_ORDERED_','_WEBSITE_ADDRESS_');
$replacements = array("_CHARSET_" => "some stupid charset");
$regex = '~(?<variable>_[A-Z_]+)~';
$tmpl = preg_replace_callback(
$regex,
function ($match) {
global $allowed, $replacements;
$m = $match["variable"];
if (in_array($m, array_keys($allowed))) {
return $replacements[$m];
// or anything else
}
},
$tmpl
);
echo $tmpl;
// now you have a stupid charset ...
?>
Upvotes: 1
Reputation: 52185
You could use an expression such as this: \$[A-Z_]+
(example here) to look for strings which start with a dollar sign ($
) and is followed by one or many upper case letters and underscores.
As pointed out by @Jan, you can use preg_replace_callback()
to have your code do some logic when replacing.
Upvotes: 1