3v4l.org

run code in 300+ PHP versions simultaneously
<?php // all keys must be lowercase $keywords_external_path = array( "business analyst là gì" => "business-analyst/", "tài liệu cho business analyst" => "tai-lieu-cho-business-analyst/", "product manager là gì" => "product-manager-la-gi/", "công việc của product manager" => "product-manager-phai-biet-dat-cau-hoi/", "QA là gì" => "qc-la-gi-qa-la-gi/", "QC là gì" => "qc-la-gi-qa-la-gi/", "Kiểm thử phần mềm là gì" => "kiem-thu-phan-mem-ai-lam-chang-duoc/", "Automation QA là gì" => "3-loi-khuyen-giup-ban-nang-cap-su-nghiep-qa/", "Tester là gì" => "tester-thanh-cong/", "kỹ năng của Tester giỏi" => "tester-thanh-cong/", "công việc QA" => "qa-gioi/", "Technical Architect là gì" => "how-to-become-ta/", ); //change the keys to lowercase (support multibyte characters) mb_internal_encoding("UTF-8"); $keywords_external_path = array_combine(array_map('mb_strtolower', array_keys($keywords_external_path)), $keywords_external_path); $data = array( 'post_content' => '"business analyst là gì" => "business-analyst/", "tài liệu cho business analyst" => "tai-lieu-cho-business-analyst/", "product manager là gì" => "product-manager-la-gi/", "công việc của product manager" => "product-manager-phai-biet-dat-cau-hoi/", "QA là gì" => "qc-la-gi-qa-la-gi/", "QC là gì" => "qc-la-gi-qa-la-gi/", "Kiểm thử phần mềm là gì" => "kiem-thu-phan-mem-ai-lam-chang-duoc/", "Automation QA là gì" => "3-loi-khuyen-giup-ban-nang-cap-su-nghiep-qa/", "Tester là gì" => "tester-thanh-cong/", "kỹ năng của Tester giỏi" => "tester-thanh-cong/", "công việc QA" => "qa-gioi/", "Technical Architect là gì" => "how-to-become-ta/"' ); $base = 'http://domain.com/'; // only if this is useful $keywords_external = array_keys($keywords_external_path); rsort($keywords_external); // to quickly discards useless position (if you have many keywords): you can also do the same with the second letter $keywords_first_letter = implode('', array_unique(array_reduce($keywords_external, function ($c, $i) { $c[] = mb_substr($i, 0, 1); return $c; }, []))); $pattern = '~\b(?=['. $keywords_first_letter . '])(?:' . implode('|', $keywords_external) . ')\b~iu'; $result = preg_replace_callback($pattern, function ($m) use ($keywords_external_path, $base) { return '<a href="' . $base . $keywords_external_path[mb_strtolower($m[0])] . '">' . $m[0] . '</a>'; }, $data['post_content']); echo $result;
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.33, 7.2.6 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.7
"<a href="http://domain.com/business-analyst/">business analyst là gì</a>" => "business-analyst/", "<a href="http://domain.com/tai-lieu-cho-business-analyst/">tài liệu cho business analyst</a>" => "tai-lieu-cho-business-analyst/", "<a href="http://domain.com/product-manager-la-gi/">product manager là gì</a>" => "product-manager-la-gi/", "<a href="http://domain.com/product-manager-phai-biet-dat-cau-hoi/">công việc của product manager</a>" => "product-manager-phai-biet-dat-cau-hoi/", "<a href="http://domain.com/qc-la-gi-qa-la-gi/">QA là gì</a>" => "qc-la-gi-qa-la-gi/", "<a href="http://domain.com/qc-la-gi-qa-la-gi/">QC là gì</a>" => "qc-la-gi-qa-la-gi/", "<a href="http://domain.com/kiem-thu-phan-mem-ai-lam-chang-duoc/">Kiểm thử phần mềm là gì</a>" => "kiem-thu-phan-mem-ai-lam-chang-duoc/", "<a href="http://domain.com/3-loi-khuyen-giup-ban-nang-cap-su-nghiep-qa/">Automation QA là gì</a>" => "3-loi-khuyen-giup-ban-nang-cap-su-nghiep-qa/", "<a href="http://domain.com/tester-thanh-cong/">Tester là gì</a>" => "tester-thanh-cong/", "<a href="http://domain.com/tester-thanh-cong/">kỹ năng của Tester giỏi</a>" => "tester-thanh-cong/", "<a href="http://domain.com/qa-gioi/">công việc QA</a>" => "qa-gioi/", "<a href="http://domain.com/how-to-become-ta/">Technical Architect là gì</a>" => "how-to-become-ta/"

preferences:
166.82 ms | 406 KiB | 203 Q