鏈接內(nèi)容變量中的所有標(biāo)記

我想鏈接content變量內(nèi)的所有標(biāo)記,我可以用foreach實現(xiàn),但是如果一個標(biāo)記是car wash,另一個是car,它在link內(nèi)創(chuàng)建鏈接,如何避免呢?

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);
    }
}

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

編輯:將已經(jīng)匹配的標(biāo)記存儲在新數(shù)組中,并在替換前與foreach和strpos檢查它們是否包含子字符串,這將是一個解決方案,但另一個問題,strpost ansstr_replace將匹配部分單詞,例如,如果標(biāo)記是car并且內(nèi)容包含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);
    }
}
? 最佳回答:

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

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

    // 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++;
    }
}
主站蜘蛛池模板: 亚洲国产一区视频| 亚洲av福利无码无一区二区| 亚洲av高清在线观看一区二区 | 韩国精品福利一区二区三区| 亚洲午夜电影一区二区三区| 亚洲一区二区三区丝袜| 日韩三级一区二区| 爆乳熟妇一区二区三区霸乳| 亚洲一区二区三区在线观看精品中文| 一区二区三区无码视频免费福利| 无码人妻一区二区三区免费看 | 四虎成人精品一区二区免费网站 | 亚洲AV无码一区二区一二区| 国产一区二区三区四| 日本亚洲国产一区二区三区| 亚洲综合一区二区精品久久| 国产嫖妓一区二区三区无码| 视频一区精品自拍| 中文字幕AV一区二区三区| 无码人妻一区二区三区免费视频| 亚洲AV综合色一区二区三区 | 亚洲国产成人一区二区三区| 久久精品一区二区三区AV| 日韩A无码AV一区二区三区 | 丰满爆乳无码一区二区三区| 一区二区三区在线观看视频| 色狠狠AV一区二区三区| 国产午夜一区二区在线观看| 久久人妻内射无码一区三区| 国产成人精品一区二三区 | 一本岛一区在线观看不卡| 午夜福利一区二区三区在线观看| 在线观看国产一区二区三区| 夜色福利一区二区三区| 一区二区三区AV高清免费波多| 日韩免费无码一区二区三区| 国产成人一区在线不卡| 天堂成人一区二区三区| 亚洲国产日韩一区高清在线| 国产福利一区二区在线视频 | 97人妻无码一区二区精品免费 |