Skip to:
Content
Pages
Categories
Search
Top
Bottom

Find words that begin with $ and create a link


  • bpnetworker
    Participant

    @bpnetworker

    Hello,

    I’m trying to find any text within user posts/activity that begin with $ and have 3-4 uppercase letters in front of it. If one is found, it would be hyperlinked to a specified page on an external site.

    // Regex
    $search = '/\$[A-Z]{3,4}/';
    
    // The Text you want to filter for urls
    $text = 'Check out these stocks $AAPL and $DRYS';
    
    for ($i = 0; $i < count(preg_match($search, $text)); $i++) {
        
        // check for word match
        if (preg_match($search, $text, $myLink)) {
            $path = "symbol";
            $tickerUrl = substr($myLink[0], 1);
            
           // create links
           echo preg_replace($search, '<a href="https://domain.com/'.$path. '/' .$tickerUrl.'">'.$myLink[0].'</a>', $text);
        
        } else {
           // if no links in the text just return the text
            echo $text;
        }
        
    } // end for loop
    
    <strong>RESULT:</strong> 
    Both mentions are linked as $AAPL ( something wrong with my loop :) ).  Link works correctly though.  Not sure how to apply this function for all buddypress activity/posts/comments.
     
    add_filter( " **NOT SURE** ", "ticker_handles" );  ??

    Any guidance is much appreciated. Thanks!

  • You must be logged in to reply to this topic.
Skip to toolbar