SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Thread: How to make replacement, only replacing once

  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2007
    Posts
    99
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to make replacement, only replacing once

    Hi guys, below are custom code to replacing text into link, for example

    Google text, will be like this Google
    Yahoo text, will be like this Yahoo

    the replacement mod below are working well until now

    now i just need a little fix again, to make it better again
    i need it now only replacing once, so not too many links if the text appear few times

    please share your knowledge guys, how to do that





    PHP Code:
    $words = array(
        
    'Google' => 'http://www.google.com',
        
    'Yahoo' => 'http://www.yahoo.com'
    );

    $exp = array();
    $rplc = array();
    foreach (
    $words as $word => $url)
    {
        
    $exp[] = '/ ' preg_quote($word) . ' /i';
        
    $rplc[] = ' <argh href="' $url '">' $word '</argh> ';
    }

        
    $this->post['message'] = preg_replace($exp$rplc$this->post['message']);
        
    $this->post['message'] = preg_replace_callback("#(<a [^>]*>)((?:[^<]|<(?!/?a>)|(?R))+)(</a>)#"create_function('$a''return $a[1] . preg_replace("#<argh[^>]*>(.*?)</argh>#", "$1", $a[2]) . $a[3];'), $this->post['message']);
        
    $this->post['message'] = preg_replace(array('#<argh #''#</argh>#'), array('<a ''</a>'), $this->post['message']); 
    Make the world better

  2. #2
    Barefoot on the Moon! silver trophy
    Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    3,658
    Mentioned
    21 Post(s)
    Tagged
    1 Thread(s)
    Use the limit parameter: PHP: preg_replace - Manual
    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •