Welcome to my website


My name is Richard Watson and I'm a professional PHP developer
from Lurgan in Northern Ireland


Creating basic BBCode in PHP

Posted by richard
News | 20-01-2016


php preg_replace regex css

Ever wondered how your favorite website handles those little codes you use to create bold or italic text, or to create links?

Rather than creating an entire hierarchy, we will look at what you can do to create hyperlinks or include images. The following code allows you to wrap [%link=url]link[/link%] around text in order to create a hyperlink or to include an image using [%image=url]alt text[/image%]. You can of course change this to [picture], [url], etc, at your own discretion.

		# Regex patterns for PHPBB-type [img=url.jpg]tag[/img] links
1: $this->pattern[0] = "/\[%link\=(.*)\](.*)\[\/link%\]/i";
2: $this->replace[0] = "<a href=\"$1\">$2</a>\r\n";
3: $this->pattern[1] = "/\[%image\=(.*)\](.*)\[\/image%\]/i";
4: $this->replace[1] = "<img src=\"$1\" title=\"$2\" alt=\"$s2\">";

# Run regex
5: $this->body = preg_replace($this->pattern[0], $this->replace[0], $this->body);
6: $this->body = preg_replace($this->pattern[1], $this->replace[1], $this->body);


See (.*) on line 2? These correspond to $1, $2 etc on line 2, in order displayed. The code simply lifts the text it finds in area 1 and transports it to $1.

Don't forget though, always parse output of user supplied data using htmlspecialchars()!


You are not signed in


Register or sign-in to post comments
Twitter
Popular tags

Test

project

files

phpmyadmin

frameworks

modal

365

new

directory

board

mysql

GPL