homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

How to make replacement, only replacing once

5+ Year Member

Msg#: 4355626 posted 6:43 pm on Aug 26, 2011 (gmt 0)

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

Google text will becoming Google [google.com]
Yahoo text will becoming Yahoo [yahoo.com]

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

$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']);



WebmasterWorld Administrator coopster us a WebmasterWorld Top Contributor of All Time 10+ Year Member

Msg#: 4355626 posted 1:29 pm on Aug 31, 2011 (gmt 0)

Check out the PCRE pages in the PHP online manual for modifiers. You can tell your regular expression not to be "greedy":

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved