鏈接內容變量中的所有標記

我想鏈接content變量內的所有標記,我可以用foreach實現,但是如果一個標記是car wash,另一個是car,它在link內創建鏈接,如何避免呢?

My code:

$tags = Tag::all();

foreach ($tags as $tag) {
    if(stripos($content, $tag->name) !== false) {
        $content = str_ireplace($tag->name, '<a href="' . route('tag.show', [$tag->id, slug($tag->name)]) . '" target="_blank">' . $tag->name . '</a>', $content);
    }
}

其中一個解決方案是按長度對標簽進行排序,從小到大,但是不,我需要一個更好的解決方案

編輯:將已經匹配的標記存儲在新數組中,并在替換前與foreach和strpos檢查它們是否包含子字符串,這將是一個解決方案,但另一個問題,strpost ansstr_replace將匹配部分單詞,例如,如果標記是car并且內容包含caring,則將存在部分匹配,并且它也將被替換,如何避免部分匹配?

編輯2:謝謝@Kinglish的幫助,最終代碼:

$tags = Tag::orderByRaw('CHAR_LENGTH(name) DESC')->get();

$linkedTags = [];
foreach ($tags as $tag) {
    foreach ($linkedTags as $linkedTag) {
        if(stripos($linkedTag, $tag->name) !== false) {
            continue 2;
        }
    }

    if(stripos($content, $tag->name) !== false) {

        $pos = stripos($content, $tag->name);
        $disallowedCharacters = [
            '-',
            '/',
            '@',
            '\\',
            '#',
            '*',
            '&'
        ];

        if ($pos != 0) {
            $charBefore = substr($content, $pos -1, 1);
            if (ctype_alpha($charBefore) || ctype_digit($charBefore) || in_array($charBefore, $disallowedCharacters)) {
                continue;
            }
        }

        $charAfter = substr($content, $pos + strlen($tag->name), 1);
        if (ctype_alpha($charAfter) || ctype_digit($charAfter) || in_array($charAfter, $disallowedCharacters)) {
            continue;
        }

        $linkedTags[] = $tag->name;
        $content = str_ireplace($tag->name, '<a href="' . route('tag.show', [$tag->id, slug($tag->name)]) . '" target="_blank">' . $tag->name . '</a>', $content);
    }
}
? 最佳回答:

好吧,有個主意也許對你有用。。。

將找到的標記存儲在一個數組中并從內容中刪除,這樣它們就不會被提取兩次。然后在最后重新組合內容。。。

    // sort array from longest to shortest 
function sort($a,$b){
    return strlen($b)-strlen($a);
}

usort($tags,'sort');



$found=array();
foreach ($tags as $tag) {
    if(stripos($content, $tag->name) !== false) {
        // test the string for partial match
        $pos = stripos($content, $tag->name);
        // test the character before
        if ($pos != 0) {
            $charBefore = substr($content, $pos -1, 1);
            if (ctype_alpha($charBefore)) {
               // its a partial, the character before the string is a letter
               continue;
            }
        }
        // test the character after
        $charAfter = substr($content, $pos + strlen($tag->name), 1);
            if (ctype_alpha($charAfter)) {
               // its a partial, the character after the string is a letter
               continue;
            }


        $storedlink = '<a href="' . route('tag.show', [$tag->id, slug($tag->name)]) . '" target="_blank">' . $tag->name . '</a>';
        $found[] = $storedlink;
        // get ref
        $ref = '[!'.count($found).'!]';
        $content = str_ireplace($tag->name, $ref, $content);
    }

    // reassemble
    $ctr=0;
    foreach ($found as $tag) {
        $content = str_replace('[!'.$ctr.'!]', $tag, $content) ;
        $ctr++;
    }
}
主站蜘蛛池模板: 国产在线观看91精品一区| 尤物精品视频一区二区三区 | 精品一区二区三区免费视频| 国产福利视频一区二区| 国产三级一区二区三区| 亚洲.国产.欧美一区二区三区 | 天堂Av无码Av一区二区三区| 久久99精品波多结衣一区| 国产一区二区三区在线电影| 亚洲午夜精品一区二区麻豆| 日本中文字幕一区二区有码在线| 激情综合丝袜美女一区二区| 国产成人精品一区二区三在线观看| 亚洲午夜精品一区二区公牛电影院| 国产乱码一区二区三区爽爽爽| 亚洲国产综合精品一区在线播放| 国产视频福利一区| 午夜视频在线观看一区二区| 亚洲av成人一区二区三区观看在线 | 一区二区三区免费视频观看| 国产成人精品无码一区二区老年人 | 中文字幕久久久久一区| 日韩精品无码Av一区二区| 国产av一区二区精品久久凹凸| 免费高清在线影片一区| 日本精品一区二区三区视频| 国产一区二区三区四| 天码av无码一区二区三区四区| 国产一区内射最近更新| 日韩精品人妻av一区二区三区| 成人免费视频一区二区| 台湾无码AV一区二区三区| 久久一本一区二区三区| 蜜桃无码一区二区三区| 色一情一乱一区二区三区啪啪高| 在线免费观看一区二区三区| 国产丝袜无码一区二区视频| A国产一区二区免费入口| 无码人妻一区二区三区免费n鬼沢| 无码国产伦一区二区三区视频| 中文字幕AV一区二区三区人妻少妇|