PDA

View Full Version : How to exclude url tage


DamasGate
10-10-2010, 06:05 PM
Hello,

I use plugin.

But i need to exclude url's "Anchor Text" inside post's to NOT effected.

Any help please?

Thank you

kh99
10-10-2010, 06:15 PM
What hook is your replacement plugin using, is it before or after bbcode replacement?

DamasGate
10-10-2010, 06:20 PM
postbit_display_complete

Thanks for help

kh99
10-10-2010, 07:21 PM
OK, there may be some more elegant way someone knows to do it (in which case, please post it), but here's what I came up with:

$word = array(
'google',
'yahoo'
);

$link = array(
'<a href="http://google.com">google</a>',
'<a href="http://yahoo.com">yahoo</a>'
);

$parts = preg_split('#(<a|</a)#i', $this->post['message'], -1, PREG_SPLIT_DELIM_CAPTURE);

$newmsg = '';
$inlink = false;
foreach ($parts as $part)
{
if (strcasecmp($part, "<a") == 0)
$inlink = true;
else if (strcasecmp($part, "</a") == 0)
$inlink = false;
else if (!$inlink)
$part = str_replace($word, $link, $part);
$newmsg .= $part;
}

$this->post['message'] = $newmsg;

I haven't tested it extensively so if you try it you might want to check a lot of posts to make sure they look OK.

DamasGate
10-10-2010, 09:44 PM
You are wonderful kh99,

Its work like a CHARM Without any error, YOU ARE GREAT believe me.
If you have a website just pm me the link with title, i will add link on my home page PR4 for your website.

Grateful for you and Thanks a lot



OK, there may be some more elegant way someone knows to do it (in which case, please post it), but here's what I came up with:

[code]I haven't tested it extensively so if you try it you might want to check a lot of posts to make sure they look OK.


I am sorry,

There is problem on images TAG
I need to exclude images IMG tags inside post's to NOT effected.

I am sorry kh99, please help me to fix it.


Thank you

kh99
10-11-2010, 07:42 PM
Yeah, I had a feeling you'd evenutally find a problem with that. It's hard to get stuff like that to be bulletproof (or at least it's hard for me).

Anyway, try this:

$word = array(
'google',
'yahoo'
);

$link = array(
'<a href="http://google.com">google</a>',
'<a href="http://yahoo.com">yahoo</a>'
);

// Match any HTML tag, this will be the delimiter in preg_split
$regexp = "/(<\/?\w+((\s+(\w|\w[\w-]*\w)(\s*=\s*(?:\".*?\"|'.*?'|[^'\">\s]+))?)+\s*|\s*)\/?>)/i";

// Capture delimiters and offsets. This will also capture things multiple times because of the
// multiple parens used in the pattern, so we'll have to skip them in the loop below
$parts = preg_split($regexp, $this->post['message'], -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_OFFSET_CAPTURE);

$newmsg = '';
$intag = false;
$offset = 0;
foreach ($parts as $part) // $part is array, 0 = string 1 = offset (because of PREG_SPLIT_OFFSET_CAPTURE)
{
if ($part[1] < $offset) // ignore parts from other parens in regexp
continue;
$offset = $part[1] + strlen($part[0]);

if (strncasecmp($part[0], '<a', 2) == 0 ||
strncasecmp($part[0], '<img', 4) == 0)
{
if (strcasecmp(substr($part[0], -2), '/>') == 0) // check for self-closed tag
$intag = false;
else
$intag = true;
}
else if (strncasecmp($part[0], '</a', 3) == 0 ||
strncasecmp($part[0], '</img', 5) == 0)
$intag = false;
else if (!$intag)
$part[0] = str_replace($word, $link, $part[0]);
$newmsg .= $part[0];
}

$this->post['message'] = $newmsg;


It's getting ugly now...

DamasGate
10-11-2010, 08:27 PM
Thats IT, :D:D:D:D

Great, I really very appreciate your patience and help,


Thank you so much kh99.

BirdOPrey5
10-11-2010, 10:47 PM
I would have searched for " google" or "google " (note the space) and replaced them with " <a href="http://google.com">google</a> " and the same for yahoo. Requiring the space as part of the string would mean www.google.com or http://google.com wouldn't be effected. The extra space introduced by the replacement is usually ignored anyway.

kh99
10-11-2010, 11:09 PM
That's a good idea, that might have solved the OP's problem. I guess since it says "Anchor Text" in the first post I was thinking the problem was the text and not the url. But it might have been a translation thing because it makes more sense that the url would be the problem.

BirdOPrey5
10-11-2010, 11:48 PM
He seems to have edited the first post, I thought this was more in line with what he originally said but now I'm not so sure.