Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
How to make replacement, only replacing once
Old 08-26-2011, 02:35 PM How to make replacement, only replacing once
Skilled Talker

Posts: 78
Name: syu
Trades: 0
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']); 
__________________
Lets help each other
basketmen is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 08-26-2011, 02:46 PM Re: How to make replacement, only replacing once
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
preg_replace() takes an optional fourth parameter that sets the limit. So does preg_replace_callback().
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.

Last edited by lizciz; 08-26-2011 at 02:47 PM..
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 08-26-2011, 09:20 PM Re: How to make replacement, only replacing once
Physicsguy's Avatar
404 - Title not found

Posts: 920
Name: Scott Kaye
Location: Ontario
Trades: 0
In case you were wondering, here's a fully working code

PHP Code:
$content "I like visiting Google more than Yahoo.  Google is way better!";
$sites = array("/Google/i","/Yahoo/i");
$reps = array("<a href=\"http://google.com\">Google</a>","<a href=\"http://yahoo.com\">Yahoo</a>");

$formatted preg_replace($sites,$reps,$content);

echo 
$formatted
Here's a link to it in action.

I can make it easier to modify if you'd like, and write a super-easy version. Assuming you're a PHP coder, this should be sufficient, although not 'correct' by some means.
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!
Physicsguy is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to How to make replacement, only replacing once
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.95184 seconds with 12 queries