php
  • html
  • 2016-08-04 5 views 0 likes 
    0

    私は長い時間テキストをデータベースから取得しています。PHPの長いURLのテキストを検出して短くする

    イムそのようにそれをフェッチ:

    echo html_entity_decode($text); 
    

    および$テキストがデータベースにそのようなものです:

    $text = "this is my best <a href='http://www.website.com/andsoooooooooooom_biiiiiiiiiiiiiiiig_characters_heeeeeeeeere'>http://www.website.com/andsoooooooooooom_biiiiiiiiiiiiiiiig_characters_heeeeeeeeere</a> and some text here"; 
    

    それは、このエコー:

    これは私の最高のhttp://www.website.com/andsoooooooooooom_biiiiiiiiiiiiiiiig_characters_heeeeeeeeereといくつかさテキストはこちら

    しかし、私は、それはURLとHREFリンクの短絡テキストを表示したいがそのように同じである:

    これは私の最高のhttp://www.website.com/andsooooooです...と、ここでいくつかのテキスト

    方法リンクテキストの長さを検出してからデータベースからフェッチすると自動的に短くなりますが、実際のリンクはそのままhrefタグにとどまりました。おかげでそんなに

    EDITイム$テキスト内のショートへのリンクを探して短い$textイムに見ていない

    答えて

    1

    使用substr()

    $text = "this is my best <a href='http://www.website.com/andsoooooooooooom_biiiiiiiiiiiiiiiig_characters_heeeeeeeeere'>http://www.website.com/andsoooooooooooom_biiiiiiiiiiiiiiiig_characters_heeeeeeeeere</a>"; 
    
    // Gets the whole tag 
    // e.g., "<a href="...">Text</a>" 
    $linkTag = getTagFromYourUglyAssString($text, "a"); 
    
    // Gets the tag's text value 
    // e.g., "Text" 
    $linkTagTextOnly = getTagTextFromYourUglyAssString($text, "a"); 
    
    // Replace $linkTag with $linkTagTextOnly, whichever string you prefer ot shorten 
    $shortenedText = substr($linkTagTextOnly,0,10).'...'; 
    
    echo $shortenedText; 
    
    function getTagFromYourUglyAssString($string, $tagname) { 
        $pattern = "/<$tagname ?.*>.*<\/$tagname>/"; 
        preg_match($pattern, $string, $matches); 
        return array_merge($matches); 
    } 
    
    function getTagTextFromYourUglyAssString($string, $tagname) { 
        $pattern = "/<$tagname ?.*>(.*)<\/$tagname>/"; 
        preg_match($pattern, $string, $matches); 
        return $matches[1]; 
    } 
    

    EDIT:実は、私はこれを考えますあなたが欲しいものです:

    $text = "this is my best <a href='http://www.website.com/andsoooooooooooom_biiiiiiiiiiiiiiiig_characters_heeeeeeeeere'>http://www.website.com/andsoooooooooooom_biiiiiiiiiiiiiiiig_characters_heeeeeeeeere</a>"; 
    
    $shortenedLink = replaceTagTextFromYourUglyAssString($text, "a"); 
    
    echo $shortenedLink; 
    // Should give: 
    // <a href="$text = "this is my best <a href='http://www.website.com/andsoooooooooooom_biiiiiiiiiiiiiiiig_characters_heeeeeeeeere"> 
    //  http://www.website.com/andsoooooooooooom_biiiiiiiiiiiiiiiig_chara... 
    // </a> 
    
    function replaceTagTextFromYourUglyAssString($string, $tagname) { 
        $pattern = "/(<$tagname ?.*>)(.*)(<\/$tagname>)/"; 
        preg_match($pattern, $string, $matches); 
    
        if (count($matches) > 0) { 
         return $matches[0].substr($matches[1],0,10).'...'.$matches[2]; 
        } else { 
         // do something else, 'cos no match found 
        } 
    } 
    
    +0

    最初に検出する方法 –

    +0

    それを検出する...?どういう意味ですか? –

    +0

    リンクのテキストを検出してからどれくらい短くしてください。あなたの例では、それはリンク全体ではなく、テキスト全体を短くします –

    関連する問題