Skip to content

Commit fe13a51

Browse files
committed
优化:title为空则从content中提取
1 parent 1a90f3c commit fe13a51

1 file changed

Lines changed: 14 additions & 1 deletion

File tree

src/Import/ArticleImport.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class ArticleImport implements ToModel, WithHeadingRow, WithValidation
1313
public function rules(): array
1414
{
1515
return [
16-
'*.title' => 'required|string|max:512',
16+
'*.title' => 'nullable|string|max:512',
1717
'*.content' => 'required|string',
1818
// '*.description' => 'nullable|string',
1919
'*.category_id' => 'nullable|integer|exists:blog_categories,id',
@@ -37,6 +37,10 @@ public function model(array $row)
3737
$row['created_at'] = date('Y-m-d H:i:s');
3838
$row['updated_at'] = date('Y-m-d H:i:s');
3939

40+
if (empty($row['title'])) {
41+
$row['title'] = self::getTitleTextByHtml($row['content']);
42+
}
43+
4044
$blogArticle = new BlogArticle($row);
4145
$blogArticle->save();
4246
}
@@ -71,4 +75,13 @@ public static function getBodyTextByHtml($html)
7175
// 去掉多余的空白字符
7276
return trim($text);
7377
}
78+
79+
public static function getTitleTextByHtml($html)
80+
{
81+
$dom = new \DOMDocument();
82+
@$dom->loadHTML($html, LIBXML_NOERROR | LIBXML_NOWARNING);
83+
$title = $dom->getElementsByTagName('title')->item(0)->nodeValue;
84+
return trim($title);
85+
}
86+
7487
}

0 commit comments

Comments
 (0)