Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

BB style string replacement... and then some 1

Status
Not open for further replies.

LatheJockey81

Technical User
Dec 23, 2004
5
US
I've successfully used str_replace(blah,blah,blah) to parse "tags" in news article posts for the website I'm working on( But now I need to take the tags a step further... This clan(Yeah... I'm in a gaming clan) has started divisions... and the divisions have their own colors. I made a function to write up the colors based on the division name, but the member's division is stored in the member database.

I made the main(news) page replace the tags [cn] and [/cn] with calls to the function to properly color the name between the tags... but it has no way of knowing HOW to color it.

I want to pull the string from between the tags and check it against the member database to determine what colors to replace the string with, and then do exactly that.

Is there a command I'm overlooking?
 
Sorry... address there is the currently working, but archaic old site... new one is still under construction so it's under the new.usoldiers.com subdomain
 
What you need to extract the [cn] tags' content is a regular expression.
Code:
<?
$text = "I made the main(news) page replace the tags [cn] and [/cn] with calls to the function to properly color the name between the tags... but it has no way of knowing HOW to color it.";
$pattern = "/\[cn\](.*?)\[\/cn\]/";
preg_match_all($pattern,$text,$result);
print_r($result);
?>
Examine the result array where you can find the content between the tag.
 
Thanks a lot... I ended up using preg_replace_callback to make the function call and replacement more streamlined... but it works :D

Not to mention I looked at the pattern description a few times and it still made almost no sense ;)

Code:
$pattern = "/\[cn\](.*?)\[\/cn\]/";
$news = preg_replace_callback($pattern,"nameanddiv",$news);
echo $news;

I know I could just echo the result... but I want to leave it open to new tags in the future
 
Do you reqlly want to know what the pattern means?
You asked for it:
Code:
/   => open pattern delimiter
\[  => escaped [, literal "[", \ needed since [ alone is a meta char in regex
cn  => literals "c" and "n"
\]  => escaped literal "]"
(   => open subpattern to be captured
.   => anything
*   => zero or more instances
?   => don't be greedy, i.e. stop when you see the next char which is "["
)   => end capture
\[  => another escaped literal "[" -> the subpatten .*? stops here
\/  => escaped literal "/"
cn  => literals "c" and "n"
\]  => escaped literal "]"
/   => end pattern delimiter
Not so difficult, eh?
 
I got the [cn] tags.. it's the subpattern that just wasn't registering. So thanks again
 
drj, thank you very much for posting that in layman's terms. star from me to you.

*cLFlaVA
----------------------------
[tt]Sigs cause cancer.[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top