Page 1 of 1
regular expressions (in PHP)
Posted: Thu Sep 22, 2005 10:27 am
by glossy
Just re-learned regular expressions since I haven't had to touch them in a while, and for some reason this isn't working, can anyone help me out?
The code is meant to get this:
array(
"sub" => "<sub>",
"edit" => "<span style=\"font-size:8pt;\">"
);
and spit out another array like so:
array(
"/sub" => "</sub>",
"/edit" => "</span>"
);
problem being, running this preg_replace on the values of the first array spits out whacked results:
$newvalue = preg_replace('@<(\w)+\s*.*>.*@i','</${1}>',$value);
resultant:
Array
(
[sub] => <sub>
[/sub] => </b>
[edit] => <span style="font-size:8pt;">
[/edit] => </n>
)
Any help would be appreciated, thanks

Posted: Thu Sep 22, 2005 11:03 am
by 4days
never think about regex's unless i've got a pack of ibuprofren on hand
this might help:
http://weitz.de/regex-coach
Posted: Thu Sep 22, 2005 11:37 am
by dmmh
i can make this much easier for you

Posted: Thu Sep 22, 2005 11:40 am
by dmmh
if you are trying to make UBB tags yourself that is, thats what I think, but Im doubting
Posted: Thu Sep 22, 2005 11:43 am
by dmmh
ill just post it, but I think this gay BB will choke on it :S
<?
function func_preg_replace_bb_code($string)
{
$patterns[0]="#\[img\](.*?)\[/img\]#si"; $replacements[0]= "<img src=\"\\1\">";
$patterns[1]="#\[url=([^\\[]*)\\]([^\\[]*)\[/url\]#si"; $replacements[1]= "<a href=\"\\1\">\\2</a>";
$patterns[2]="#\[quote\](.*?)\[/quote\]#si"; $replacements[2]= "<span class=\"quote\">\\1</span>";
$patterns[3]="#\[google\](.*?)\[/google\]#si"; $replacements[3]= "<a href=\"http://www.google.com/search?q=\\1\" target=\"_blank\">\\1</a>";
$patterns[4]="#\[b\](.*?)\[/b]#si"; $replacements[4]= "<strong>\\1</strong>";
$patterns[5]="#\[i\](.*?)\[/i]#si"; $replacements[5]= "<i>\\1</i>";
$patterns[6]="#\[img=(.*?)\]\[/img\]#si"; $replacements[6]= "<a href=\"\\1\"><img src=\"\\1\" border=\"0\" title=\"click to download\" alt=\"image\"></a>";
ksort($patterns);
ksort($replacements);
return preg_replace($patterns, $replacements, $string);
}?>
call like: <? echo func_preg_replace_bb_code($some_huge_string);?>
Posted: Thu Sep 22, 2005 11:44 am
by dmmh
never mind, this aint what u need

Posted: Thu Sep 22, 2005 11:48 am
by dmmh
also, dont forget
http://www.webmasterworld.com , it has a lot of accumulated knowledge
ps: regex sux

Posted: Thu Sep 22, 2005 1:57 pm
by glossy
yes i'm doing BBcode, and yes that's similar, except i'd like to be able to have it possible to input it easily into the database, like:
'bbcode' => 'blink'
'code' => '<blink>'
and have my closing tags done automagically...
stupid regex

Posted: Thu Sep 22, 2005 3:41 pm
by dmmh
mmmm, ill think with u tonight, gtg work soon
btw, I developed a way to destroy people's session via my admin control panel. Kinda neat when you catch someone form re-submitting or suspect him of other bad stuff
user gets immediate notice (well, on new page or page refresh)

Posted: Thu Sep 22, 2005 6:56 pm
by phoq
I only had 5 minutes, so I altered your regular match expression and replace expression.
Code: Select all
$match = '@(<?)(\w+)(\s*)([a-zA-Z0-9=\"-/\s:;]*)(>?)@';
$replace = '${1}/${2}${5}';
$newvalue = preg_replace($match,$replace,$value);
I'm no php expert (can one actually be an expert at a language that simple?) and I didn't have the time to figure out how to change the array keys as well.
The expression works on unbracketed keywords as well (so you can replace the keys in the array), so for example "img" will be replaced by "/img" and
will be replaced by
The reason for your strange output is the fact that you replaced the match with just one character from the original input (\w).
Posted: Thu Sep 22, 2005 6:59 pm
by phoq
I can speak a whole lot of nerd in just one breath

Posted: Fri Sep 23, 2005 7:59 am
by glossy
phoq wrote:I only had 5 minutes, so I altered your regular match expression and replace expression.
Code: Select all
$match = '@(<?)(\w+)(\s*)([a-zA-Z0-9="-/\s:;]*)(>?)@';
$replace = '${1}/${2}${5}';
$newvalue = preg_replace($match,$replace,$value);
I'm no php expert (can one actually be an expert at a language that simple?) and I didn't have the time to figure out how to change the array keys as well.
The expression works on unbracketed keywords as well (so you can replace the keys in the array), so for example "img" will be replaced by "/img" and
will be replaced by
The reason for your strange output is the fact that you replaced the match with just one character from the original input (\w).
!! thank you so much phoq, and the reason behind it mucking up is clear to me now, thanks for helping me out.
And yes, PHP is quite a simple language but it gets the job done, and is easy to get straight from concept to working application, which is great when you have hardly any free time.
Thanks again for your help

Posted: Fri Sep 23, 2005 9:12 am
by phoq
np mate