returning part of a string

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
ruud
Forum Newbie
Posts: 16
Joined: Mon Oct 27, 2003 9:57 am

returning part of a string

Post 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 :)
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post 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.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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)
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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.
pistacchio
Forum Newbie
Posts: 5
Joined: Wed Sep 01, 2004 5:59 am

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
Post Reply