Merge pull request #22532 from seamuslee001/dev_core_3034
[civicrm-core.git] / CRM / Core / Smarty / plugins / modifier.crmReplace.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * Replace the value of an attribute in the input string. Assume
20 * the the attribute is well formed, of the type name="value". If
21 * no replacement is mentioned the value is inserted at the end of
22 * the form element
23 *
24 * @param string $string
25 * The html to be tweaked with.
26 * @param string $attribute
27 * The attribute to modify.
28 * @param string $value
29 * The new attribute value.
30 *
31 * @return string
32 * the new modified html string
33 */
34 function smarty_modifier_crmReplace($string, $attribute, $value) {
35 // we need to search and replace the string: $attribute=XXX or $attribute="XXX"
36 // with $attribute=\"$value\"
37 $pattern = '/' . $attribute . '="([^"]+?)"/';
38 return preg_replace($pattern, $attribute . '="' . $value . '"', $string);
39 }