Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
CakePHP从纯文本电子邮件中删除HTML_Php_Cakephp - Fatal编程技术网

CakePHP从纯文本电子邮件中删除HTML

CakePHP从纯文本电子邮件中删除HTML,php,cakephp,Php,Cakephp,当在我的cakephp应用程序2.2中添加新文章时,我正在使用email Component向订阅的人发送电子邮件 我使用TinyMCE允许管理员用户格式化文本,这会导致一些格式化HTML保存在数据库中,这很好,但是我想以电子邮件格式向被选中的用户发送整篇文章的电子邮件,当添加新文章时,包括HTML和纯文本。这对于html版本很好,但是如何在保留html版本的同时从纯文本版本中剥离html?以下是我目前的代码: public function admin_add() { if ($thi

当在我的cakephp应用程序2.2中添加新文章时,我正在使用email Component向订阅的人发送电子邮件

我使用TinyMCE允许管理员用户格式化文本,这会导致一些格式化HTML保存在数据库中,这很好,但是我想以电子邮件格式向被选中的用户发送整篇文章的电子邮件,当添加新文章时,包括HTML和纯文本。这对于html版本很好,但是如何在保留html版本的同时从纯文本版本中剥离html?以下是我目前的代码:

public function admin_add() {
    if ($this->request->is('post')) {
        $this->NewsArticle->create();
        if ($this->NewsArticle->save($this->request->data)) {

            // If is a tech alart - send email
            if ($this->request->data['NewsCategory']['NewsCategory']['0'] == '2') {

                // Email users who have opted in to webform updates
                $usersToEmail = $this->NewsArticle->query("SELECT username, tech_email FROM users WHERE tech_email = 1");

                // Loop throughh all opt'ed in users
                foreach ($usersToEmail as $user) {

                    $this->Email->template = 'newTechAlert';
                    $this->Email->from    = 'Client Area <clientarea@someurl.co.uk>';
                    $this->Email->to      = $user['users']['username'];
                    $this->Email->subject = 'New Technical Alert';
                    // Send as both HTML and Text
                                            $this->Email->sendAs = 'both';

                    // Set vars for email
                    $this->set('techAlertTitle', $this->request->data['NewsArticle']['title']);

                    ##  NEED TO STRIP THE HTML OUT FOR NONE HTML EMAILS HERE - BUT HOW???
                    $this->set('techAlertBody', $this->request->data['NewsArticle']['body']);


                    $this->set('user', $user['users']['username']);
                    $this->Email->send();

                }

            }
您可以使用php strip_标记方法

您还可以向函数传递第二个参数,使其仍然允许换行或href标记。

您可以使用php strip_tags方法

您还可以将第二个参数传递给函数,使其仍然允许换行或href标记。

我使用:

$this->Email->emailFormat('both');

// Convert <br> to \n
$text = preg_replace('/<br(\s+)?\/?>/i', "\n", $html);
// Remove html markup
$text = trim(strip_tags($text));
// Replace multiple (one ore more) line breaks with a single one.
$text = preg_replace("/(\r\n|\r|\n)+/", "\n", $text);

$this->Email->viewVars(compact('text', 'html'));
我使用:


啊,当然很简单。我想我今天早上需要更多的咖啡!谢谢你,哈哈,第二杯来了。这就是我得到答案的原因。澄清:删除HTML标记可能会破坏基本的段落格式和换行符。例如:Foo

Bar

->FooBar。这就是为什么我提到了strip_标签函数的第二个参数,但是或者在纯文本中没有意义:-?啊,当然很简单。我想我今天早上需要更多的咖啡!谢谢你,哈哈,第二杯来了。这就是我得到答案的原因。澄清:删除HTML标记可能会破坏基本的段落格式和换行符。例如:Foo

Bar

->FooBar。这就是为什么我提到了strip|u标签函数的第二个参数,但它在纯文本中没有意义:-?如果你想保留段落,但由于html标记仍然过滤3-xxx换行:$text=preg|u replace/\r\n |\r\n |\r\n |\r\n+/,\n\r\n,$text;如果您想保留段落,但由于html标记仍要筛选3-xxx个换行符,则还可以将两个以上的换行符替换为两个换行符:$text=preg_replace/\r\n | \r\n | \r\n | \r | \n+/,\n\n,$text;
$this->Email->emailFormat('both');

// Convert <br> to \n
$text = preg_replace('/<br(\s+)?\/?>/i', "\n", $html);
// Remove html markup
$text = trim(strip_tags($text));
// Replace multiple (one ore more) line breaks with a single one.
$text = preg_replace("/(\r\n|\r|\n)+/", "\n", $text);

$this->Email->viewVars(compact('text', 'html'));
$this->Email->reset();