如何将错误消息添加到我的错误消息<ul> in php </ul>

时间:2011-11-17 05:21:06

标签: php codeigniter

我正在使用codeigniter。我从codeigniter表单验证中获得的错误生成自定义错误消息:

$this->message->set_error ( $this->form_validation->error_array () );
$message = $this->message->get_message ();
echo "<pre>"; print_r($message); die;
$this->data ['message'] = $message;

$message就是这个

<div class="errormsg message">
<p>
<em>Error</em>
</p>
<ul>
<li>The First Name field is required.</li>
<li>The Last Name field is required.</li>
<li>The Date of birth field is required.</li>
<li>The Email address field is required.</li>
<li>The Password field is required.</li>
<li>The Password Confirmation field is required.</li>
<li>The City field is required.</li>
<li>The State field is required.</li>
<li>The captureCode field is required.</li>
</ul>
</div>

我有另一个错误字符串

$err = 'Age is below 14';

我想将此$err添加到<ul> 因此生成的<ul>将是

<ul>
<li>The First Name field is required.</li>
<li>The Last Name field is required.</li>
<li>The Date of birth field is required.</li>
<li>The Email address field is required.</li>
<li>The Password field is required.</li>
<li>The Password Confirmation field is required.</li>
<li>The City field is required.</li>
<li>The State field is required.</li>
<li>The captureCode field is required.</li>
<li>Age is below 14</li>
</ul>

这样做的最佳方式是什么?

1 个答案:

答案 0 :(得分:3)

您可以使用简单的字符串替换来代替使用DOMDocument之类的东西来附加元素:

$message = '<div class="errormsg message">
<p>
<em>Error</em>
</p>
<ul>
<li>The First Name field is required.</li>
</ul>
</div>';
$err = 'Age is below 14';
$message = str_replace( '</ul>', '<li>' . $err . '</li></ul>', $message);

Demo