Bài 6: Tìm kiếm, thay thế và lưu lại
Bài học hướng dẫn này cung cấp các ví dụ về chỉnh sửa nội dung liên quan đến tìm kiếm và thay thế, sau đó lưu lại tài nguyên vào cơ sở dữ liệu.
Extension PHP Functions via Schema cung cấp các trường "tìm kiếm và thay thế" sau:
_strReplace: Thay thế một chuỗi bằng một chuỗi khác_strReplaceMultiple: Thay thế một danh sách chuỗi bằng một danh sách chuỗi khác_strRegexReplace: Tìm kiếm chuỗi cần thay thế bằng biểu thức chính quy_strRegexReplaceMultiple: Tìm kiếm các chuỗi cần thay thế bằng một danh sách biểu thức chính quy_strArrayReplace: Thay thế một chuỗi bằng một chuỗi khác trong một mảng_strArrayReplaceMultiple: Thay thế một danh sách chuỗi bằng một danh sách chuỗi khác trong một mảng
Tìm kiếm và thay thế một chuỗi
Query GraphQL này lấy một bài viết, thay thế tất cả các lần xuất hiện của một chuỗi bằng một chuỗi khác trong nội dung và tiêu đề của bài viết, rồi lưu lại bài viết:
query GetPostData(
$postId: ID!
$replaceFrom: String!,
$replaceTo: String!
) {
post(by: { id: $postId }) {
title
adaptedPostTitle: _strReplace(
search: $replaceFrom
replaceWith: $replaceTo
in: $__title
)
@export(as: "adaptedPostTitle")
rawContent
adaptedRawContent: _strReplace(
search: $replaceFrom
replaceWith: $replaceTo
in: $__rawContent
)
@export(as: "adaptedRawContent")
}
}
mutation UpdatePost($postId: ID!)
@depends(on: "GetPostData")
{
updatePost(input: {
id: $postId,
title: $adaptedPostTitle,
contentAs: { html: $adaptedRawContent },
}) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
post {
id
title
rawContent
}
}
}Để thực thi query, chúng ta cung cấp từ điển variables với các chuỗi cần tìm kiếm và thay thế:
{
"postId": 1,
"replaceFrom": "Old string",
"replaceTo": "New string"
}Tìm kiếm và thay thế nhiều chuỗi
Đây là query tương tự ở trên, nhưng bằng cách sử dụng _strReplaceMultiple, chúng ta có thể thay thế một danh sách chuỗi bằng một danh sách chuỗi khác:
query GetPostData(
$postId: ID!
$replaceFrom: [String!]!,
$replaceTo: [String!]!
) {
post(by: { id: $postId }) {
title
adaptedPostTitle: _strReplaceMultiple(
search: $replaceFrom
replaceWith: $replaceTo
in: $__title
)
@export(as: "adaptedPostTitle")
rawContent
adaptedRawContent: _strReplaceMultiple(
search: $replaceFrom
replaceWith: $replaceTo
in: $__rawContent
)
@export(as: "adaptedRawContent")
}
}
mutation UpdatePost($postId: ID!)
@depends(on: "GetPostData")
{
updatePost(input: {
id: $postId,
title: $adaptedPostTitle,
contentAs: { html: $adaptedRawContent },
}) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
post {
id
title
rawContent
}
}
}Từ điển variables bây giờ nhận một danh sách các chuỗi cần tìm kiếm và thay thế:
{
"postId": 1,
"replaceFrom": ["Old string 2", "Old string 2"],
"replaceTo": ["New string1", "New string 2"]
}Thêm các liên kết còn thiếu
Query GraphQL này thực hiện tìm kiếm và thay thế bằng regex để thêm các liên kết còn thiếu vào nội dung HTML của bài viết:
query GetPostData($postId: ID!) {
post(by: { id: $postId }) {
id
rawContent
adaptedRawContent: _strRegexReplace(
searchRegex: "#\\s+((https?)://(\\S*?\\.\\S*?))([\\s)\\[\\]{},;\"\\':<]|\\.\\s|$)#i"
replaceWith: "<a href=\"$1\" target=\"_blank\">$3</a>$4"
in: $__rawContent
)
@export(as: "adaptedRawContent")
}
}
mutation UpdatePost($postId: ID!)
@depends(on: "GetPostData")
{
updatePost(input: {
id: $postId,
contentAs: { html: $adaptedRawContent },
}) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
post {
id
title
rawContent
}
}
}Tất cả các URL không được bao quanh bởi thẻ anchor, chẳng hạn như:
<p>Visit my website: https://mysite.com.</p>...sẽ được thêm thẻ <a> tương ứng xung quanh chúng (đồng thời xóa tên miền khỏi văn bản và thêm target để mở trong cửa sổ mới), trở thành:
<p>Visit my website: <a href="https://mysite.com" target="_blank">mysite.com</a>.</p>- Ký tự
"\"phải được thoát thành"\\"bên trong mẫu regex. Ví dụ,"/^https?:\/\//"được viết thành"/^https?:\\/\\//" - Tài liệu về hàm PHP
preg_replacegiải thích cách sử dụng tham chiếu thay thế (ví dụ:$1) và bộ chỉnh sửa PRCE.
Thay thế HTTP bằng HTTPS
Query GraphQL này thay thế tất cả các URL http bằng https trong các nguồn ảnh HTML:
query GetPostData($postId: ID!) {
post(by: {id: $postId}) {
id
rawContent
adaptedRawContent: _strRegexReplace(
searchRegex: "/<img(\\s+)?([^>]*?\\s+?)?src=([\"'])http:\\/\\/(.*?)/"
replaceWith: "<img$1$2src=$3https://$4$3"
in: $__rawContent
)
@export(as: "adaptedRawContent")
}
}
mutation UpdatePost($postId: ID!)
@depends(on: "GetPostData")
{
updatePost(input: {
id: $postId,
contentAs: { html: $adaptedRawContent },
}) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
post {
id
title
rawContent
}
}
}