y2ksw
02-08-2013, 02:26 PM
I'm currently building a much needed Autolinker for vBulletin 4 (much like GAL) for release on vbulletin.org, and have the following issues with PCRE and replacing contents outside HTML tags:
Words with accents are not recognized as full words, thus a replacement for 'pi?' with 'x' results in 'xx' for 'pi?pi?'. But at most I can live without it.
Rules are not considering the tail of a string, so I must add a character ad the end, and remove later (and this, too, is a minor concern)However, before realeasing a hack instead proper code, I would like to ask a PCRE professional for help on this problem. Here below is the code ready to paste into a php file:
<?php
function pcre_cb($matches)
{
return 'x';
}
$subject = 'pi? o pi?pi? o meno 1 <a href="abc.it">pi? o pi?pi? o meno 2</a> pi? o pi?pi? o meno 3 <a href="abc.it">pi? o pi?pi? o meno 4</a> pi? o pi?pi? o meno 5';
$pattern = '#\\b(pi?|abc\\.it)(?![^<]*>*(</|$))#im';
$text = preg_replace_callback($pattern, 'pcre_cb', $subject . '<');
echo nl2br(htmlspecialchars($text));
die;Result:
x o xx o meno 1 <a href="abc.it">pi? o pi?pi? o meno 2</a> x o xx o meno 3 <a href="abc.it">pi? o pi?pi? o meno 4</a> x o xx o meno 5<Wanted:
x o pi?pi? o meno 1 <a href="abc.it">pi? o pi?pi? o meno 2</a> x o pi?pi? o meno 3 <a href="abc.it">pi? o pi?pi? o meno 4</a> x o pi?pi? o meno 5
Words with accents are not recognized as full words, thus a replacement for 'pi?' with 'x' results in 'xx' for 'pi?pi?'. But at most I can live without it.
Rules are not considering the tail of a string, so I must add a character ad the end, and remove later (and this, too, is a minor concern)However, before realeasing a hack instead proper code, I would like to ask a PCRE professional for help on this problem. Here below is the code ready to paste into a php file:
<?php
function pcre_cb($matches)
{
return 'x';
}
$subject = 'pi? o pi?pi? o meno 1 <a href="abc.it">pi? o pi?pi? o meno 2</a> pi? o pi?pi? o meno 3 <a href="abc.it">pi? o pi?pi? o meno 4</a> pi? o pi?pi? o meno 5';
$pattern = '#\\b(pi?|abc\\.it)(?![^<]*>*(</|$))#im';
$text = preg_replace_callback($pattern, 'pcre_cb', $subject . '<');
echo nl2br(htmlspecialchars($text));
die;Result:
x o xx o meno 1 <a href="abc.it">pi? o pi?pi? o meno 2</a> x o xx o meno 3 <a href="abc.it">pi? o pi?pi? o meno 4</a> x o xx o meno 5<Wanted:
x o pi?pi? o meno 1 <a href="abc.it">pi? o pi?pi? o meno 2</a> x o pi?pi? o meno 3 <a href="abc.it">pi? o pi?pi? o meno 4</a> x o pi?pi? o meno 5