繁体   English   中英

PHP正则表达式替换文本中的关键字,但不在锚标记中

[英]PHP regex replace keyword in text but not inside anchor tags

我正在尝试为关键字实现自动超链接功能。

我遇到的问题是关键字可以成为其他关键字的一部分。 例如:马铃薯,地瓜。 该功能必须知道不要在甘薯中超链接马铃薯。

我正在使用正则表达式,它实际上可以在不同的环境下工作,但不适用于我的本地主机和实时版本。

工作示例:

$keywords_external = array(
  "Sweet potato" => "food/sweet-potato", 
  "Potato salads" => "food/potato-salads",
  "Potato" => "food/potato", 
);

$data = array(
  'post_content' => 'Sweet potato some text then potato then more text and then potato salads'
);

foreach($keywords_external as $key => $href) 
{
  $regex = '/<a\b(?=\s)(?:[^>=]|=\'[^\']*\'|="[^"]*"|=[^\'"\s]*)*"\s?>.*?<\/a>|('.$key.')/ims';

  $data['post_content'] = preg_replace_callback(
    $regex,
    function ($matches) {
        if (array_key_exists (1, $matches)) {
           return '<a href="https://example.com/">'. $matches[1] .'</a>';
        }
        return $matches[0];
    },
    $data['post_content']
  );
}

echo $data['post_content'];

适用于http://phpfiddle.org

同一示例在实时版本中不起作用。

有什么想法可以以不同的方式实现相同的目标,或者为什么在实时版本中可能行不通?

谢谢。

PHP版本
Localhost:PHP版本7.0.12,
实时:PHP版本7.0.15-1 + deb.sury.org〜xenial + 1,
实时运行:PHP版本5.4.45

[edit]:由于您正在处理多字节字符,因此需要对代码进行一些编辑:

我不知道您的确切问题是什么,但这是我要解决的方式(一次通过):

// 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://yourdomain.com/'; // only if this is useful

$keywords_external = array_map(function ($i) {
    return preg_quote($i, '~');
}, array_keys($keywords_external_path));
rsort($keywords_external);

// to quickly discard useless positions (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 = '~'
         . '(?=['. preg_quote($keywords_first_letter, '~') . '])'
         . '(?=\b\w|(?<!\S)\W)'
         . '(?:' . implode('|', $keywords_external) . ')'
         . '(?<=\w\b|\W(?!\S))~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;

演示了几个PHP版本

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM