/    Sign up×
Community /Pin to ProfileBookmark

using REGEX to avoid preg_replace of links

hi, the following code replaces all keywords with a linked keyword. However, I would like to use REGEX in order to avoid the manipulation of keywords when they fall within links.

ex: if the keyword is orange

<br>orange</br> would become

<br><a href = “somelink”>orange</a></br>

however I’d like to avoid the following

original string:
<a href = “http://somedomain.com/orange”>yadayada</a>

string after preg_replace:
<a href = “http://somedomain.com/<a href = “somelink”>orange</a>”>yadayada</a>

code:

[code]
global $mainframe;

$content = $article -> text;
$current = JURI::current();

$keywords = $this -> params->def(‘keywords’);
$words = explode(“,”, $keywords);

$keywordLinks = $this -> params->def(‘links’);
$Links = explode(“,”, $keywordLinks);

$number = count($words);

for($i = 0; $i < $number; $i++)
{
$Links[$i] = ‘<a href = “../plugins/content/keyword.php?destination=’.urlencode($Links[$i]).’&location=’.urlencode($current).’&keyword=’.$words[$i].'”>’.$words[$i].'</a>’;

}

for($i = 0; $i < $number; $i++)
{
$words[$i] = ‘/b’.$words[$i].’b/’;
/*$words[$i] = str_replace(‘ ‘, ”, $words[$i]);*/
}

$content = preg_replace($words, $Links, $content);

$article -> text = $content;
[/code]

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@MrTripiauthorJun 17.2009 — actually, I think it's the slash in the urls which is throwing the current pattern off.


reviews/payroll-relief-from-accountantsworld.html

payroll would be replaced in the above string, maybe it's due to the slash preceding the keyword(payroll)??
×

Success!

Help @MrTripi spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.7,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...