I have string in my database like
Label is {input:inputvalue} and date is {date:2013-2-2}
How can I extract input and inputvalue from the first brace, and date and 2013-2-3 from the second brace and so on? So that displays like
Label is <input name="input" value="input_value"> and date is <input name="date" value="2013-2-2">
Below function works only if the string has {input} or {date}
function Replace_brackets($rec){
$arr = array(" <input name="input" value='input'> ",
" <input name="date" value='date'> ");
$arr1 = array('{input}','{date}');
$itemvalue=str_replace($arr1,$arr,$rec);
return $itemvalue;
}
There might be more or less braces on the text such as 2 input braces and 4 date braces.
Any ideas?
preg_replace() with back references will work in this case http://php.net/manual/en/function.preg-replace.php:
Or if you need to parse the name and value pairs, as @Jack pointed out, you could use the preg_replace_callback() version (you don’t actually need to use htmlspecialchars() on the attribute values though. Replace htmlspecialchars() with whatever parsing function is applicable):