Page 1 of 1
[SOLVED]Regular Expressions
Posted: Sun Feb 01, 2004 8:05 am
by vigge89
Are there any sites where i can find tutorials/guides related to Regular Expression in PHP?
Also, are there any sites which have ready-to-use RegExps?
What i am looking for is patterns to create links, make text bold, italic, etc (BBCode style)...
Posted: Sun Feb 01, 2004 8:52 am
by timvw
http://www.php.net/manual -> perl regular expressions. There are some links mentionned.
I still haven't found a real good expressions parser, so i'm about to write one myself.
But if all you want to is parse phpBB tags, you could use of the phpBB parser package in PEAR.
Posted: Sun Feb 01, 2004 9:55 am
by vigge89
Ok, i've made this function from the phpBB BBCode-files:
Code: Select all
<?php
function format_string ($string) { ########## function format_string() ##########
if (! (strpos ($string, '[') && strpos ($string, ']')) ) { // if no code-blocks was found
return $string; // skip formatting
} else { // if code-blocks was found
########## String replacing ##########
// [color=black]black_text[/color]
$string = preg_replace ("/\[color=(\#[0-9A-F]{6}|[a-z]+):$uid\]/si", "<span style='color: \\1'>", $string);
$string = str_replace ("[/color:$uid]", "</span>", $string);
// [b]bold_text[/b]
$string = str_replace ("[b:$uid]", "<span style='font-weight: bold'>", $string);
$string = str_replace ("[/b:$uid]", "</span>", $string);
// [u]underlined_text[/u]
$string = str_replace ("[u:$uid]", "<span style='text-decoration: underline'>", $string);
$string = str_replace ("[/u:$uid]", "</span>", $string);
// [i]italic_text[/i]
$string = str_replace ("[i:$uid]", "<span style='font-style: italic'>", $string);
$string = str_replace ("[/i:$uid]", "</span>", $string);
########## /String replacing ##########
########## Regular Expressions ##########
$patterns = array();
$replacements = array();
####### patterns & replacements #######
// [img]image_url_here.ext[/img]
$patterns[] = "#\[img:$uid\](.*?)\[/img:$uid\]#si";
$replacements[] = "<img src='\\1' />";
// [url]xxxx://www.domain.com[/url]
$patterns[] = "#\[url\]([\w]+?://[^ "\n\r\t<]*?)\[/url\]#is";
$replacements[] = "<a href='\\1' target='_blank'>\\1</a>";
// [email]user@domain.com[/email]
$patterns[] = "#\[email\]([a-z0-9&\-_.]+?@[\w\-]+\.([\w\-\.]+\.)?[\w]+)\[/email\]#si";
$replacements[] = "<a href='mailto:\\1'>\\1</a>";
###### replace bbcode with real code ######
$string = preg_replace ($patterns, $replacements, $string);
########## /Regular Expressions ##########
return $string;
} // if code-blocks was found
} ########## function format_string() ##########
?>
but when i run it (after file have been included) by using
$pres = format_string ($pres);
no formatting is done, nothing happens with the text!
Posted: Sun Feb 01, 2004 10:08 am
by markl999
if (! (strpos ($string, '[') && strpos ($string, ']')) ) {
That will only work as long as [ isn't the first char in the file.
And the code is riddled with :$uid , and $uid isn't defined anywhere.. ?
Try the below
Code: Select all
<?php
function format_string ($string) { ########## function format_string() ##########
if ((strpos ($string, 'ї') === FALSE) || (strpos ($string, ']') === FALSE)) { // if no code-blocks was found
echo 'No code blocks found!';
return $string; // skip formatting
} else { // if code-blocks was found
########## String replacing ##########
$uid = '';
// black_text
$string = preg_replace ("/\їcolor=(\#ї0-9A-F]{6}|їa-z]+)\]/si", "<span style='color: \\1'>", $string);
$string = str_replace ("ї/color]", "</span>", $string);
// bold_text
$string = str_replace ("їb]", "<span style='font-weight: bold'>", $string);
$string = str_replace ("ї/b]", "</span>", $string);
// underlined_text
$string = str_replace ("їu]", "<span style='text-decoration: underline'>", $string);
$string = str_replace ("ї/u]", "</span>", $string);
// italic_text
$string = str_replace ("їi]", "<span style='font-style: italic'>", $string);
$string = str_replace ("ї/i]", "</span>", $string);
########## /String replacing ##########
########## Regular Expressions ##########
$patterns = array();
$replacements = array();
####### patterns & replacements #######
// їimg]image_url_here.extї/img]
$patternsї] = "#\їimg\](.*?)\ї/img\]#si";
$replacementsї] = "<img src='\\1' />";
// xxxx://www.domain.com
$patternsї] = "#\їurl\](ї\w]+?://ї^ "\n\r\t<]*?)\ї/url\]#is";
$replacementsї] = "<a href='\\1' target='_blank'>\\1</a>";
// user@domain.com
$patternsї] = "#\їemail\](їa-z0-9&\-_.]+?@ї\w\-]+\.(ї\w\-\.]+\.)?ї\w]+)\ї/email\]#si";
$replacementsї] = "<a href='mailto:\\1'>\\1</a>";
###### replace bbcode with real code ######
$string = preg_replace ($patterns, $replacements, $string);
########## /Regular Expressions ##########
return $string;
} // if code-blocks was found
}
?>
Posted: Sun Feb 01, 2004 10:18 am
by vigge89
got it to worknow, $uid was a random variable ID in phpBB, but i thought it was required. thanks!
