get a specific value from a string with preg_match

Ever needed a specific value from a string? I show you how to do it with preg_match…

Within FormBuilder module template, i need a specific attribute value of an input field.

Imagine this is our sample input field:

<input id="fbrp__31" type="text" maxlength="80" size="25" value="Nic" name="cntnt01fbrp__31" disabled="">

I want to get the value from attribute value="". This would be simply "Nic". To achieve this, we need a new UDT. Create it, call it "preg_match" and paste in the following code:

$input_string = $params["var"];
$target = $params["target"];
$assign = $params["assign"];
$count = preg_match('/'.$target.'=(["\'])(.*?)\1/', $input_string, $match);
if (isset($params["assign"])){
  $smarty->assign($assign,$match[2]);
}else{
  echo $match[2];
}

Now we can retreive the value assuming the input string is within a specific variable (here $fields.fbrp__42->input)

{preg_match var=$fields.fbrp__42->input target="value" assign="result"}{$result}

That's all. You can leave assign paramter if you don't need it. You can also get the value of the name attribute like this:

{preg_match var=$fields.fbrp__42->input target="name"}

This would output: cntnt01fbrp__31

Now it's on you what you do with it. Have fun…

Comments