Go Back   Webmaster Malaysia Forum » Website Design & Development » Website Programming

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-12-2004, 03:24 PM
heterozigot's Avatar
Novice Webmaster
 
Join Date: Jan 2003
Location: PJ
Posts: 51
Rep Power: 70
heterozigot is on a distinguished road
Send a message via ICQ to heterozigot Send a message via Yahoo to heterozigot
camne nak replace?

PHP CODE:

ini aku tak mau replace <test>aku nak replace ayat di antara dua menda ni</test> dan ini aku tak mau replace........

sapa tau? tolong tunjukan. Mcm BB code tu.
__________________
Try and Support Malaysia Product.
http://foto.home.net.my
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 01-12-2004, 10:34 PM
Novice Webmaster
 
Join Date: Sep 2004
Location: Shah Alam
Posts: 68
Rep Power: 50
infeeneetee is on a distinguished road
From: http://www.tutorio.com/tutorial/simp...bcode-with-php

Part 1: Simple bb code

This techinque for simple bb code can be used to easily replace tags with no attributes like the bold and italic tags.

PHP Code:
$string "I am so [b]cool[/b] "
$bb-replace = array('[b]','[/b]','[i]',[/i]);
$bb-replacements = array ('','','','');
$string str_replace($bb-replace,$bb-replacements,$string); 
Two arrays are defined one for the bb code and another one for the html that will replace the bbcode and then the str_replace function is used to replace the html text for the bbcode in the string.

The same technique may also be used for things such as creating a swear filters, smilies and emoticons etc..

Part 2: Complex BB code

The previous technique for creating bb code works well for things like bold and italic tags, but its not good for creating more complex things like bb code for hyperlinks or image tags.

PHP Code:
$string 'This is [b]cool[/b] - [url=http://www.tutorio.com]Tutorio.com Tutorials[/url] 
$bb-replace = array ('
/([[Bb]])(.+)([/[Bb]])/','/([url=)(.+)(])(.+)([/url])/');
$bb-replacements = array ('
<b>2</b>','<a href="\\2">4</a>);
$string preg_replace($bb-replace$bb-replacements$string);
print 
$string
This is somewhat similar to the first example in that it still contains two arrays one containing the information for the bb code you need to replace and the other for the html text that will replace it. The difference is that this time instead of using the str_replace, this technique use preg_replace and regex. In this example the BBcode for bold text and the URL hyperlinks is replaced with the correct html tags.

Regex is a very powerful (and somewhat confusing) set of rules to create patterns for string manipulation, if you are into programming you are probably familiar with it already. The BBcode replacement pattern used for the url tags in this tutorial can be broken down into 5 parts, each part is enclosed within a round bracket. The preg function will find and replace either the exact text in each of these round brackets or match the text to the rules in these brackets.

(\[url=) This part matches '[url=' in the BBcode. the reason There is a backslash in front of the square brackets is that square brackets a special charecter in regex. Note that parts 3 and 5 work in the same kind of way.

(.+) This part would tell regex what characters to match, the dot matches almost every character and the + sign will means the number of characters has to be greater than zero, its used in parts 2 and 4. The reason it is used here is because we don't know the URL or the name of the page that the user is going to link to, .

<a href="\\2">\\4</a> Here parts 2 and 4 (url and title) are extracted and put inside an HTML hyperlink which replaces the BBcode.

BBcode is very popular in PHP scripts such as forums guestbooks and shoutboxes that wish to allow some user styling while limiting the potential for abuse that comes with giving users full access to HTML

Last edited by infeeneetee; 01-12-2004 at 10:45 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-12-2004, 10:07 AM
heterozigot's Avatar
Novice Webmaster
 
Join Date: Jan 2003
Location: PJ
Posts: 51
Rep Power: 70
heterozigot is on a distinguished road
Send a message via ICQ to heterozigot Send a message via Yahoo to heterozigot
preg_replace function, use regular expression.

ok thanks.
__________________
Try and Support Malaysia Product.
http://foto.home.net.my
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
display data,camne??? adib Website Programming 14 21-08-2004 10:27 AM
camne nak masuk DB?? adib Website Programming 2 11-06-2004 06:55 PM
VB 6.0: Camne nak save checkbox value PrincessLea Webmaster Tools 3 08-06-2004 04:00 PM
camne nak bagi div nie tinggi 100%? b3njo Website Design 1 01-10-2003 11:14 PM
camne nak publish web gune php silverX Website Programming 7 20-11-2002 12:06 PM



All times are GMT +8. The time now is 09:31 AM. Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO 3.1.0 vBulletin skin by ForumMonkeys.com.


WebmasterMalaysia.com is Proudly Hosted by Exabytes Semi Dedicated Server.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59