I want to replace the following code:
[text required id="address_line_1" label="Address Line 1"]
[text required id="city" label="City"]
With this:
<label for="address_line_1">Address Line 1 (required)</label>
<input type="text" name="address_line_1" />
<label for="city">City (required)</label>
<input type="text" name="city" />
I think preg_replace is the best option? But I have no idea how to do this since the content I want to replace is all mixed in and it's not just a simple replace X with Y string.
Appreciate any help!
You could use preg_match like that (this works for both your cases), but you need to do it for each line of your example separately:
<?php
function transformToHTML($input) {
$regex = '/\[(text)\s+(required)?\s+id="(.*)?"\s+label="(.*)"\s*\]/i';
$replaced = $input; // default, when there's no match
if (preg_match($regex, $input, $matches)) {
$inputType = $matches[1];
$id = $matches[3];
$required = $matches[2];
$label = trim($matches[4] . ' ' . ($required ? "({$required})" : ''));
$replaced = '<label for="' . $id . '">' . $label . '</label>
<input type="' . $inputType . '" name="' . $id . '" />';
}
return $replaced;
}
$input = '[text required id="address_line_1" label="Address Line 1"]';
$output = transformToHTML($input);
echo "INPUT: {$input}<br>";
// remove "htmlentities()" call to get raw html string
// here it's just for printing HTML string into HTML ;)
echo "OUTPUT: " . htmlentities($output);
It prints (tested on PHPFiddle):
INPUT: [text required id="address_line_1" label="Address Line 1"]
OUTPUT: <label for="address_line_1">Address Line 1 (required)</label> <input type="text" name="address_line_1" />
Of course this is valid for your cases and possibly you'll need to adjust it more if you have some additional input types or more rules. Still - fiddle around with regexp and everything is possible :)
What I would do is explode the content and use it at my will. I know you asked a regex, but every answer here in SO I'm trying to do without that, sometimes it's more legible:
function mountHtml($text = '[text required id="address_line_1" label="Address Line 1"]')
{
$text = str_replace(['[', ']'],'',$text);
$items = explode(' ', $text);
$items[3] = str_replace(['label="', '"'], '', implode(' ', array_slice($items, 3)));
$items = array_splice($items, 0, 4);
$label = [
str_replace('id', 'for', $items[2]),
$items[3]
];
$input = [
$items[0],
str_replace('id', 'name', $items[2]),
$items[2]
];
$htmlStructure = "<label $label[0]>$label[1] ($items[1])</label>";
$htmlStructure .= "<input type='$input[0]' $input[1] $input[2] />";
return $htmlStructure;
}
echo mountHtml();
//Result
<label for="address_line_1">Address Line 1 (required)</label><input type="text" name="address_line_1" id="address_line_1">
PS: I observed that you didn't add id into input structure, but it's important to do that for better DOM management, because every label with for is linked to an structure with an ID, it's a W3C standard.
for = string Specified to indicate a form control with which the caption is to be associated. The attribute's value must be the ID of a labelable form-associated element in the same Document as the label element.