/    Sign up×
Community /Pin to ProfileBookmark

preg_replace url rel’s

hi all, id like to add nofollows onto my links on my forum…

basicly before the skin is printed, id like to replace my links and add the rel=”nofollow” attribute…

ive seen the many tutorials on how to make your [link] into <a etc.. but im not sure how to apply the knwoledge to this situation…im not really changing much, just adding rel=”nofollow” into the link…

help?

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@NightShift58Jan 21.2007 — help?[/quote]code?
Copy linkTweet thisAlerts:
@deep-woodauthorJan 23.2007 — thats the thing, i have no idea what do write?

all im wanting to add rel="nofollow" before the > where the code code before it was <a href="(whatever..)"

from what ive tried to learn from various articles and stuff, id guess..

preg_replace("&lt;a href="(.+?)"&gt;(.+?)&lt;/a&gt;",&lt;a href="$1" rel="nofollow"&gt;$2&lt;/a&gt;",$test)

is that a start?
Copy linkTweet thisAlerts:
@NogDogJan 23.2007 — Untested:
[code=php]
$test = preg_replace('/(<a[srn]+[^>]+)>/i', '\1 rel="nofollow">', $test);
[/code]
Copy linkTweet thisAlerts:
@deep-woodauthorJan 23.2007 — Untested:
[code=php]
$test = preg_replace('/(<a[srn]+[^>]+)>/i', '\1 rel="nofollow">', $test);
[/code]
[/QUOTE]

wow nodog thats amazing!

would you mind if you explain what all the diff bits mean? im yet to find a good tutorial on regex and looking at your code makes me go wow! lol

thankyou kindly
Copy linkTweet thisAlerts:
@NogDogJan 23.2007 — <i>
</i>'/(&lt;a[srn]+[^&gt;]+)&gt;/i' :

/ = start regex pattern
( = start sub-pattern
&lt;a = &lt;a
[srn]+ = one or more occurrences of any white-space, CR, or LF characters
[^&gt;]+ = one or more occurrences of any character that's not a "&gt;"
) = end of sub-pattern
<i>&gt; </i>= &gt;
/ = end of pattern
i = case-Insensitive modifier

The "1" in the replacement means whatever was captured by the first (and only in this case) sub-pattern.
Copy linkTweet thisAlerts:
@bokehJan 23.2007 — [code][srn]+ = one or more occurrences of any white-space, CR, or LF characters[/QUOTE]I probably would have used a word boundrary here ([B]b[/B]) but that's by the by; what is fact though is the character class is redundant in this case. [B]s[/B] matches ASCII characters 9, 10, 12, 13, 32 and 160. So CR and LF (ASCII 10 and 13 respectively) are already covered by [B]s[/B].
×

Success!

Help @deep-wood 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 6.17,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...