[SOLVED]Regular Expressions

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

[SOLVED]Regular Expressions

Post 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)...
Last edited by vigge89 on Sun Feb 01, 2004 10:19 am, edited 1 time in total.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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.
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post 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!
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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

&lt;?php
function format_string ($string) { ########## function format_string() ##########
if ((strpos ($string, '&#1111;') === 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 ("/\&#1111;color=(\#&#1111;0-9A-F]{6}|&#1111;a-z]+)\]/si", "&lt;span style='color: \\1'&gt;", $string);
$string = str_replace ("&#1111;/color]", "&lt;/span&gt;", $string);

// bold_text
$string = str_replace ("&#1111;b]", "&lt;span style='font-weight: bold'&gt;", $string);
$string = str_replace ("&#1111;/b]", "&lt;/span&gt;", $string);

// underlined_text
$string = str_replace ("&#1111;u]", "&lt;span style='text-decoration: underline'&gt;", $string);
$string = str_replace ("&#1111;/u]", "&lt;/span&gt;", $string);

// italic_text
$string = str_replace ("&#1111;i]", "&lt;span style='font-style: italic'&gt;", $string);
$string = str_replace ("&#1111;/i]", "&lt;/span&gt;", $string);

########## /String replacing ##########

########## Regular Expressions ##########

$patterns = array();
$replacements = array();

####### patterns &amp; replacements #######

// &#1111;img]image_url_here.ext&#1111;/img]
$patterns&#1111;] = "#\&#1111;img\](.*?)\&#1111;/img\]#si";
$replacements&#1111;] = "&lt;img src='\\1' /&gt;";

// xxxx://www.domain.com
$patterns&#1111;] = "#\&#1111;url\](&#1111;\w]+?://&#1111;^ "\n\r\t&lt;]*?)\&#1111;/url\]#is";
$replacements&#1111;] = "&lt;a href='\\1' target='_blank'&gt;\\1&lt;/a&gt;";

// user@domain.com
$patterns&#1111;] = "#\&#1111;email\](&#1111;a-z0-9&amp;\-_.]+?@&#1111;\w\-]+\.(&#1111;\w\-\.]+\.)?&#1111;\w]+)\&#1111;/email\]#si";
$replacements&#1111;] = "&lt;a href='mailto:\\1'&gt;\\1&lt;/a&gt;";

###### replace bbcode with real code ######
$string = preg_replace ($patterns, $replacements, $string);

########## /Regular Expressions ##########

return $string;

} // if code-blocks was found

}
?&gt;
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

got it to worknow, $uid was a random variable ID in phpBB, but i thought it was required. thanks! :D
Post Reply