บทเรียนที่ 6: ค้นหา แทนที่ และจัดเก็บอีกครั้ง
บทเรียนในทูทอเรียลนี้นำเสนอตัวอย่างการปรับเปลี่ยนเนื้อหาที่เกี่ยวข้องกับการค้นหาและแทนที่ แล้วจัดเก็บทรัพยากรกลับเข้าสู่ DB
ส่วนขยาย PHP Functions via Schema มอบฟิลด์ "ค้นหาและแทนที่" ต่อไปนี้:
_strReplace: แทนที่สตริงด้วยอีกสตริงหนึ่ง_strReplaceMultiple: แทนที่รายการสตริงด้วยรายการสตริงอีกชุดหนึ่ง_strRegexReplace: ค้นหาสตริงที่ต้องการแทนที่โดยใช้นิพจน์ปกติ (regular expression)_strRegexReplaceMultiple: ค้นหาสตริงที่ต้องการแทนที่โดยใช้รายการนิพจน์ปกติ_strArrayReplace: แทนที่สตริงด้วยอีกสตริงหนึ่งภายในอาร์เรย์_strArrayReplaceMultiple: แทนที่รายการสตริงด้วยรายการสตริงอีกชุดหนึ่งภายในอาร์เรย์
ค้นหาและแทนที่สตริง
GraphQL query นี้ดึงข้อมูลโพสต์ แทนที่ทุกครั้งที่พบสตริงหนึ่งด้วยอีกสตริงหนึ่งในเนื้อหาและชื่อเรื่องของโพสต์ แล้วจัดเก็บโพสต์อีกครั้ง:
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
}
}
}ในการรัน query เราจะระบุพจนานุกรมของ variables พร้อมสตริงที่ต้องการค้นหาและแทนที่:
{
"postId": 1,
"replaceFrom": "Old string",
"replaceTo": "New string"
}ค้นหาและแทนที่หลายสตริง
นี่คือ query เดียวกับด้านบน แต่การใช้ _strReplaceMultiple ทำให้เราสามารถแทนที่รายการสตริงด้วยรายการสตริงอีกชุดหนึ่งได้:
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
}
}
}พจนานุกรมของ variables ตอนนี้จะรับรายการสตริงที่ต้องการค้นหาและแทนที่:
{
"postId": 1,
"replaceFrom": ["Old string 2", "Old string 2"],
"replaceTo": ["New string1", "New string 2"]
}การเพิ่มลิงก์ที่ขาดหายไป
GraphQL query นี้ทำการค้นหาและแทนที่ด้วยนิพจน์ปกติ เพื่อเพิ่มลิงก์ที่ขาดหายไปในเนื้อหา HTML ของโพสต์:
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
}
}
}ทุก URL ที่ไม่ได้ถูกล้อมรอบด้วยแท็ก anchor เช่น:
<p>Visit my website: https://mysite.com.</p>...จะถูกเพิ่มแท็ก <a> ที่สอดคล้องกันล้อมรอบไว้ (พร้อมทั้งลบโดเมนออกจากข้อความ และเพิ่ม target เพื่อเปิดในหน้าต่างใหม่) กลายเป็น:
<p>Visit my website: <a href="https://mysite.com" target="_blank">mysite.com</a>.</p>- อักขระ
"\"ต้องถูก escape เป็น"\\"ภายในรูปแบบนิพจน์ปกติ ตัวอย่างเช่น"/^https?:\/\//"จะถูกเขียนเป็น"/^https?:\\/\\//" - เอกสารสำหรับ ฟังก์ชัน PHP
preg_replaceอธิบายวิธีใช้ การอ้างอิงการแทนที่ (replacement references) (เช่น$1) และ ตัวปรับแต่ง PRCE (PRCE modifiers)
การแทนที่ HTTP ด้วย HTTPS
GraphQL query นี้แทนที่ URL ที่เป็น http ทั้งหมดด้วย https ในแหล่งที่มาของรูปภาพ 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
}
}
}