Page 1 of 1

returning part of a string

Posted: Wed Sep 01, 2004 5:14 am
by ruud
Hi there

I'm a bit of a PHP part timer so if this is stupidly obvious then forgive me. Hopefully someone can help me! I want to return part of string a that i'm lifting out of a forum db table.

eg - text that is in the db

[color=red:5da1c4d043]Team Leader: [b:5da1c4d043]Ashley Peake[/color:5da1c4d043

I want to display all the text between the end ] and the beginning [ so i'm not displaying any of the forum tags.

so for the example text above i just want to display on my web page - 'Team Leader ' and 'Ashley Peake'

I've been trying explode but that doesn't seem to be suitable.

Many thanks for any help i get :)

Posted: Wed Sep 01, 2004 5:34 am
by anjanesh
These are not normal tags. So you need to write your own tag parser for such cases. But if you're getting this from phpBB then I bet the parser is already written.

Posted: Wed Sep 01, 2004 5:37 am
by timvw
http://pear.php.net/package/HTML_BBCodeParser can be extended easily to do what you want (and it's quite easy to strip out the bloated stuff, so you don't need a whole pear installation)

Posted: Wed Sep 01, 2004 6:00 am
by CoderGoblin
Or if you want to do it yourself look up regular expressions in the php manual:

http://de3.php.net/manual/en/ref.pcre.php (preferred).
http://de3.php.net/manual/en/ref.regex.php

You need to be careful after all what happens to missing start/close tags or if the user has a 'tag' within the text.

Posted: Wed Sep 01, 2004 6:12 am
by pistacchio
i find regx really uneasy to use. i'm facing the same problem of ruud. writing a while loop to look for "]", advance one letter at a time till the next "[" is easy, but resource consuming, i think (from server and speed point of view). a regx expression could do the work, but i never used them. could someone help? in my case the delimiters are "<[" and "]>" (so that "<[Foobar]>" would return Foobar only). Is there a method as well to take the hole tag (" Hi there is some <[Foobar]> here" would return "<[Foobar]>".

What I'm doing is a very basic muti-layer file-based template engine. Every html part in a php code or every php part in a html file is substituded with tags like the ones mentioned. The code loops throu the includes and put codes token from files (say foobar.inc) in the place of the tags, than go thour it again and again (in case include files have tags withing them) till no more tags are found. than it makes a php file with the long text generated (whose name is user's session id), and it sends back to the browser as the requested page.

tia
gu

Posted: Wed Sep 01, 2004 9:59 am
by feyd
the regular expressions to do this have been posted many times, under differing subjects.

personally, I'd use [php_man]preg_replace_callback[/php_man], or [php_man]preg_replace[/php_man] with an execution directive.