regular expressions (in PHP)

Open discussion about any topic, as long as you abide by the rules of course!
Post Reply
glossy
Posts: 2285
Joined: Tue Apr 30, 2002 7:00 am

regular expressions (in PHP)

Post 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 :)
4days
Posts: 5465
Joined: Tue Apr 16, 2002 7:00 am

Post 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
dmmh
Posts: 2501
Joined: Thu Jan 04, 2001 8:00 am

Post by dmmh »

i can make this much easier for you :)
[i]And shepherds we shall be, for thee my Lord for thee, Power hath descended forth from thy hand, that our feet may swiftly carry out thy command, we shall flow a river forth to thee, and teeming with souls shall it ever be. In nomine patris, et fili, et spiritus sancti.[/i]
dmmh
Posts: 2501
Joined: Thu Jan 04, 2001 8:00 am

Post by dmmh »

if you are trying to make UBB tags yourself that is, thats what I think, but Im doubting
[i]And shepherds we shall be, for thee my Lord for thee, Power hath descended forth from thy hand, that our feet may swiftly carry out thy command, we shall flow a river forth to thee, and teeming with souls shall it ever be. In nomine patris, et fili, et spiritus sancti.[/i]
dmmh
Posts: 2501
Joined: Thu Jan 04, 2001 8:00 am

Post 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);?>
Last edited by dmmh on Thu Sep 22, 2005 11:46 am, edited 1 time in total.
[i]And shepherds we shall be, for thee my Lord for thee, Power hath descended forth from thy hand, that our feet may swiftly carry out thy command, we shall flow a river forth to thee, and teeming with souls shall it ever be. In nomine patris, et fili, et spiritus sancti.[/i]
dmmh
Posts: 2501
Joined: Thu Jan 04, 2001 8:00 am

Post by dmmh »

never mind, this aint what u need :(
[i]And shepherds we shall be, for thee my Lord for thee, Power hath descended forth from thy hand, that our feet may swiftly carry out thy command, we shall flow a river forth to thee, and teeming with souls shall it ever be. In nomine patris, et fili, et spiritus sancti.[/i]
dmmh
Posts: 2501
Joined: Thu Jan 04, 2001 8:00 am

Post by dmmh »

also, dont forget http://www.webmasterworld.com , it has a lot of accumulated knowledge

ps: regex sux :D
[i]And shepherds we shall be, for thee my Lord for thee, Power hath descended forth from thy hand, that our feet may swiftly carry out thy command, we shall flow a river forth to thee, and teeming with souls shall it ever be. In nomine patris, et fili, et spiritus sancti.[/i]
glossy
Posts: 2285
Joined: Tue Apr 30, 2002 7:00 am

Post 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 :(
dmmh
Posts: 2501
Joined: Thu Jan 04, 2001 8:00 am

Post 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 :D

user gets immediate notice (well, on new page or page refresh) :)
[i]And shepherds we shall be, for thee my Lord for thee, Power hath descended forth from thy hand, that our feet may swiftly carry out thy command, we shall flow a river forth to thee, and teeming with souls shall it ever be. In nomine patris, et fili, et spiritus sancti.[/i]
phoq
Posts: 238
Joined: Wed May 03, 2000 7:00 am

Post 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

Code: Select all

<img src=gsfsfs width=33123>
will be replaced by

Code: Select all

</img>
The reason for your strange output is the fact that you replaced the match with just one character from the original input (\w).
Last edited by phoq on Thu Sep 22, 2005 7:01 pm, edited 1 time in total.
phoq
Posts: 238
Joined: Wed May 03, 2000 7:00 am

Post by phoq »

I can speak a whole lot of nerd in just one breath :up:
glossy
Posts: 2285
Joined: Tue Apr 30, 2002 7:00 am

Post 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

Code: Select all

<img src=gsfsfs width=33123>
will be replaced by

Code: Select all

</img>
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 :)
phoq
Posts: 238
Joined: Wed May 03, 2000 7:00 am

Post by phoq »

np mate
Post Reply