Php 从函数驱动的html生成格式正确的源代码
我有一个方法,用于从属性数组生成用户表单 该函数可以很好地满足我的需要,但是当查看源代码时,它是一个连续的行,而不是更可读的缩进格式 以下是生成的源代码:Php 从函数驱动的html生成格式正确的源代码,php,html,forms,oop,Php,Html,Forms,Oop,我有一个方法,用于从属性数组生成用户表单 该函数可以很好地满足我的需要,但是当查看源代码时,它是一个连续的行,而不是更可读的缩进格式 以下是生成的源代码: <form><input name=songName type=text placeholder=Title </input><input name=songArtist type=text placeholder=Artist </input><input name=songGenre
<form><input name=songName type=text placeholder=Title </input><input name=songArtist type=text placeholder=Artist </input><input name=songGenre type=text placeholder=Genre </input><input name=year type=number placeholder=Year </input></form>
您只需在代码中添加相应的缩进和换行,如下所示:
public function formGenerate($formElements)
{
echo "<form>\n"; // adding the \n for form
foreach($formElements as $name=>$properties)
{
echo " <input "."name=".$name." ";
$propertiesArray = explode('|',$properties);
foreach($propertiesArray as $property)
{
$split = $this->splitPropertyAndValue($property);
$propertyName = $split['property'];
$propertyValue = $split['value'];
echo $propertyName.'='.$propertyValue." ";
}
echo ">\n"; // self closing tag!
}
echo "</form>";
}
公共函数formGenerate($formElements)
{
echo“\n”;//为表单添加\n
foreach($formElements作为$name=>$properties)
{
echo“\n”//自动关闭标记!
}
回声“;
}
在回音陈述中使用\t
和\n
。。。玩转它,你会得到你需要的:)fyi:输入标记是自动关闭标记,不管怎样,你有一个示例数组值$formElements
?
public function formGenerate($formElements)
{
echo "<form>";
foreach($formElements as $name=>$properties)
{
echo "<input "."name=".$name." ";
$propertiesArray = explode('|',$properties);
foreach($propertiesArray as $property)
{
$split = $this->splitPropertyAndValue($property);
$propertyName = $split['property'];
$propertyValue = $split['value'];
echo $propertyName.'='.$propertyValue." ";
}
echo "</input>";
}
echo "</form>";
}
$formElements = array
(
'songName' => 'type:text|placeholder:Title',
'songArtist' => 'type:text|placeholder:Artist',
'songGenre' => 'type:text|placeholder:Genre',
'year' => 'type:number|placeholder:Year'
);
public function formGenerate($formElements)
{
echo "<form>\n"; // adding the \n for form
foreach($formElements as $name=>$properties)
{
echo " <input "."name=".$name." ";
$propertiesArray = explode('|',$properties);
foreach($propertiesArray as $property)
{
$split = $this->splitPropertyAndValue($property);
$propertyName = $split['property'];
$propertyValue = $split['value'];
echo $propertyName.'='.$propertyValue." ";
}
echo ">\n"; // self closing tag!
}
echo "</form>";
}