Re: [PHP] Re: BBCode?
Thanks a lot, but do you know how to do the same with ereg_replace? I'm trying to learn PCRE, but until I do, I won't be able to add anything to this... ?php function bbcode($text){ $text = ereg_replace((\\[i\\])([^\\[]+)(\\[/i\\]),'i\\2/i',$text); return $text; } print bbcode('[i]This[/i] is a [i]test[/i].'); ? Works... -- Kyle Gibson admin(at)frozenonline.com http://www.frozenonline.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: BBCode?
Thanks, but that doesn't allow nested tags (a [b] tag inside of an [i] tag, for example). Kyle Gibson wrote: Thanks a lot, but do you know how to do the same with ereg_replace? I'm trying to learn PCRE, but until I do, I won't be able to add anything to this... ?php function bbcode($text){ $text = ereg_replace((\\[i\\])([^\\[]+)(\\[/i\\]),'i\\2/i',$text); return $text; } print bbcode('[i]This[/i] is a [i]test[/i].'); ? Works... -- The above message is encrypted with double rot13 encoding. Any unauthorized attempt to decrypt it will be prosecuted to the full extent of the law. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: BBCode?
On Tuesday 26 November 2002 01:42, Leif K-Brooks wrote: Thanks, but that doesn't allow nested tags (a [b] tag inside of an [i] tag, for example). If you want to deal with nested tags then it's probably best to replace each opening and closing tag individually. Otherwise you might end up with some fiendishly complicated regex. Did I say I preferred the PCRE functions to the EREG ones? Well using preg_replace() you can put all your search strings in one array and all the corresponding replacement strings in another and it'll replace all those tags for you in a single operation: $text = '[i]This[/i] is a [i]test[/i].'; $search_for_tags = array(/\[i\]/, /\[\/i\]/); $replace_with_tags = array(i, /i); $text = preg_replace($search_for_tags, $replace_with_tags, $text); print $text; -- Jason Wong - Gremlins Associates - www.gremlins.biz Open Source Software Systems Integrators * Web Design Hosting * Internet Intranet Applications Development * /* Ignorance must certainly be bliss or there wouldn't be so many people so resolutely pursuing it. */ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: BBCode?
This will replace all versions (open and close) of bold, italicize and underline tags with brackets using preg_replace. $text = preg_replace(/\[(\/)?([biu])\]/,\\1\\2,$text) ; definition - find [ = \[ - match IF slash exists = (\/)? - match one of the following = ([biu]) - find ] = \] - matches get put into 'memory' (\\1 \\2) even if not found (ie. slash didn't exist). - replace with 'first match' 'second match' - NOTE : in PCRE regexs, brackets and forward-slashes are special characters and must be escaped (preceded with a back-slash) when looking for them as regular characters. /definition Even if you do not know PCRE yet, don't hamstring yourself looking for a solution that is too complicated and time-consuming. EREG does not work very well on this type of problem. Buck up, learn PCRE and use this type of solution (if you find something that works for you, use it...by no means does the code above cover every tag you 'may' want to use). Builder.com has a pretty good regular expression checker for PERL-style regexs that make PCRE pretty easy. http://builder.com - Go To: Web Scripting (left menu) - Cool Tools (right menu) - Regular Expression Inspector (2/3 way down page) .mike === Michael Geier CDM Sports, Inc. Systems Administration email: [EMAIL PROTECTED] phone: 314.991.1511 x 6505 --- This email sent using CDM Sports Webmail v3.1 [ http://webmail.cdmsports.com ] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: BBCode?
Thanks, but that doesn't allow nested tags (a [b] tag inside of an [i] tag, for example). Nested tags? Do you mean [ib] ? Or [i[b]]? If you mean [ib], then I'm afraid what you want cannot be done in ereg_replace. As far as I am aware, that is. -- Kyle Gibson admin(at)frozenonline.com http://www.frozenonline.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: BBCode?
On Tuesday 26 November 2002 04:42, Kyle Gibson wrote: Thanks, but that doesn't allow nested tags (a [b] tag inside of an [i] tag, for example). Nested tags? Do you mean [ib] ? Or [i[b]]? I think he means: [i]A sentence in italics, with a word in [b]bold[/b][/i] -- Jason Wong - Gremlins Associates - www.gremlins.biz Open Source Software Systems Integrators * Web Design Hosting * Internet Intranet Applications Development * /* The more I know men the more I like my horse. */ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: BBCode?
I think he means: [i]A sentence in italics, with a word in [b]bold[/b][/i] Yeah he told me. -- Kyle Gibson admin(at)frozenonline.com http://www.frozenonline.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: BBCode?
Thanks, but that doesn't allow nested tags (a [b] tag inside of an [i] tag, for example). Try this: ? function format_text($text) { $open=\\[; $close=\\]; $pattern=$open.(.).$close.([^.$open.]+).$open./.(.).$close; $temp = $text; $text = ereg_replace($pattern,'\1\2/\3',$text); while($temp != $text) { $temp = $text; $text = ereg_replace($pattern,'\1\2/\3',$text); } return $text; } print format_text('[i] this is a [b]bold[/b] test [/i]'); ? -- Kyle Gibson admin(at)frozenonline.com http://www.frozenonline.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: BBCode?
on 26/11/02 7:42 AM, Kyle Gibson ([EMAIL PROTECTED]) wrote: Thanks, but that doesn't allow nested tags (a [b] tag inside of an [i] tag, for example). Nested tags? Do you mean [ib] ? Or [i[b]]? I would have thought nested tags would have meant: [i]something something [b]bolder[/b] something[/i], since ib and ib aren't valid HTML, can't see why they'd have been adopted by BBcode. Justin French http://Indent.com.au Web Development Graphic Design -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: BBCode?
Leif K-Brooks wrote: I'm working on adding simple BBCode to my site. I'm currently using the [i] tag for testing, with the following code: ?php function bbcode($text){ $text = ereg_replace('\\[i\\](.{1,})\\[/i\\]','i\\1/i',$text); return $text; } print bbcode('[i]This[/i] is a [i]test[/i].'); ? But it prints iThis[/i] is a [i]test/i. Is there a better way to do this? ? function bbcode($text) { $text = str_replace([,,$text); $text = str_replace(],,$text); return $text; } print bbcode([i]This[/i] is a [i]test[/i].); ? -- Kyle Gibson admin(at)frozenonline.com http://www.frozenonline.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: BBCode?
Thanks, but that isn't what I want. The point behind BBCode is allowing a small, simpler, securer, subset of HTML. I.e. [img=http://domain.com/image.gif] changes to img src=http://domain.com/image.gif; alt= / Kyle Gibson wrote: Leif K-Brooks wrote: I'm working on adding simple BBCode to my site. I'm currently using the [i] tag for testing, with the following code: ?php function bbcode($text){ $text = ereg_replace('\\[i\\](.{1,})\\[/i\\]','i\\1/i',$text); return $text; } print bbcode('[i]This[/i] is a [i]test[/i].'); ? But it prints iThis[/i] is a [i]test/i. Is there a better way to do this? ? function bbcode($text) { $text = str_replace([,,$text); $text = str_replace(],,$text); return $text; } print bbcode([i]This[/i] is a [i]test[/i].); ? -- The above message is encrypted with double rot13 encoding. Any unauthorized attempt to decrypt it will be prosecuted to the full extent of the law. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: BBCode?
On Monday 25 November 2002 14:20, Kyle Gibson wrote: Leif K-Brooks wrote: I'm working on adding simple BBCode to my site. I'm currently using the [i] tag for testing, with the following code: ?php function bbcode($text){ $text = ereg_replace('\\[i\\](.{1,})\\[/i\\]','i\\1/i',$text); return $text; } print bbcode('[i]This[/i] is a [i]test[/i].'); ? But it prints iThis[/i] is a [i]test/i. Is there a better way to do this? It's not working because it's doing a greedy match. Try (I prefer the PCRE functions over EREG): $text = preg_replace(/\[i\](.*)\[\/i\]/U, i$1/i, $text); ? function bbcode($text) { $text = str_replace([,,$text); $text = str_replace(],,$text); return $text; } print bbcode([i]This[/i] is a [i]test[/i].); ? This code is simply not suitable. The object is to replace tags as an entity as opposed to simply replacing '[' with ''. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: BBCode?
Thanks, but that isn't what I want. The point behind BBCode is allowing a small, simpler, securer, subset of HTML. I.e. [img=http://domain.com/image.gif] changes to img src=http://domain.com/image.gif; alt= / In that case: ?php function bbcode($text){ $text = ereg_replace((\\[i\\])([^\\[]+)(\\[/i\\]),'i\\2/i',$text); return $text; } print bbcode('[i]This[/i] is a [i]test[/i].'); ? Prints: iThis/i is a itest/i. -- Kyle Gibson admin(at)frozenonline.com http://www.frozenonline.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: BBCode?
This code is simply not suitable. The object is to replace tags as an entity as opposed to simply replacing '[' with ''. Obviously not. I was not fully aware of what he required. -- Kyle Gibson admin(at)frozenonline.com http://www.frozenonline.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: BBCode?
Thanks a lot, but do you know how to do the same with ereg_replace? I'm trying to learn PCRE, but until I do, I won't be able to add anything to this... Jason Wong wrote: It's not working because it's doing a greedy match. Try (I prefer the PCRE functions over EREG): $text = preg_replace(/\[i\](.*)\[\/i\]/U, i$1/i, $text); -- The above message is encrypted with double rot13 encoding. Any unauthorized attempt to decrypt it will be prosecuted to the full extent of the law. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php