html - PHP preg_replace commas select option
Get the solution ↓↓↓How can I replace commas with brakes in select option to change from this:
To this:
It seems that my current code doesent work anymore:
// $row[1] = "enum('1','2','3','4','5','6','7','8','9','10')";
$options = explode("', '", preg_replace("/(enum|set)\('(.+?)'\)/", "\\2", $row[1]));
Full code from first image:
$row = $db->query("SHOW COLUMNS FROM namemap LIKE 'multiplier'");
$row = $row->fetch_row();
$options = explode("', '", preg_replace("/(enum|set)\('(.+?)'\)/", "\\2", $row[1]));
print("<tr><td align='left' class='header'>Multiplier:</td>");
print("<td align='left' class='lista' colspan='2'><select name='multiplier'>");
foreach($options as $multiplier) {
$option = "<option ";
if ($multiplier == $results['multiplier'])
$option .= "selected=selected ";
$option .= "value=".$multiplier.">" . unesc($multiplier) . "</option>";
print($option);
}
print("</select></td></tr>");
Answer
Solution:
Here is how I separated the enum string. Not the most elegant thing, but it works:
$options = explode(",",str_replace("'","", str_replace('enum(', '', str_replace(')','', $row[1]))));
Also I got rid of theprint()
statements. You can output the html directly - separating the html presentation from the php logic helps make the code a little more legible and flexible.
<?php
$row = array("multiplier","enum('1','2','3','4','5','6','7','8','9','10')","YES","","1","");
$options = explode(",",str_replace("'","", str_replace('enum(', '', str_replace(')','', $row[1]))));
?>
<tr><td align='left' class='header'>Multiplier:</td>
<td align='left' class='lista' colspan='2'><select name='multiplier'>
<?php foreach($options as $multiplier) {
$s='';
if ($multiplier == $results['multiplier']) $s="selected";
?>
<option value='<?php echo $multiplier?>' <?php echo $s?>><?php echo $multiplier?></option>
<? } ?>
</select></td></tr>
Answer
Solution:
I think you found the correct place where it is not working any longer:
$options = explode("', '", preg_replace("/(enum|set)\('(.+?)'\)/", "\\2", $row[1]));
The reason is that the split-string "', '
" contains a space after the comma while the string returned frompreg_replace
does not have spaces after the comma.
So using explode with a fixed string does not suffice any longer as entering these values in the database may be with or without space(s) after a comma (and perhaps even before?).
Once seen, the fix could be straight forward. One solution is to use a regular expression pattern as well when "exploding", there is a sister function for that withpreg_replace
and it is called :
$options = preg_split("/',\s*'/", ...);
^^^
\s : space character
* : zero or more, as many as possible (greedy)
So exchangingexplode
withpreg_split
can upgrade the code to handle cases with zero or more space characters after the comma.
Likewise you could introduce potential space characters also before the comma by allowing them the same, just before the comma.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: the requested url was not found on this server. xampp
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.