Solved

Explain this preg_replace

Posted on 2004-08-26
6
455 Views
Last Modified: 2010-10-05
Thanks to Nomad :http://experts-exchange.com/Web/Web_Languages/PHP/Q_21101766.html

this code works well for bold.  Now I need it for italics...
I have attempted to figure it out, but I come here to the experts.
Could someone give me the down and dirty of this code.  I know php OK, but not all the rules.


$text = preg_replace('/\\[b:[0-9a-f]+\\](.*)\\[\\/b:[0-9a-f]+\\]/Usi', '<b>\\1</b>', $announcement);

this is the code now showing.


[i:33cc0ee790]Paranoia[/i:33cc0ee790])

thank s
Rockman
0
Comment
Question by:rockmansattic
  • 3
  • 2
6 Comments
 
LVL 27

Expert Comment

by:Diablo84
ID: 11908659
just a simple moddification to

$text = preg_replace('/\\[i:[0-9a-f]+\\](.*)\\[\\/i:[0-9a-f]+\\]/Usi', '<i>\\1</i>', $announcement);

should work
0
 
LVL 27

Accepted Solution

by:
Diablo84 earned 425 total points
ID: 11908827
sorry just re-read question

/ start of pattern

[i: matches the start of the string from your pattern [i:

[0-9a-f]+ accepts one or more characters limited to 0-9 and a through to f

] matchs the end part of [i; after the numbers ]

(.*) brackets signify a group which is referenced later, 0 or more of any character

thats the general structure of the pattern

notice when the tags are replaced with <i>\\1</i>  \\1 represents the group above, can also be done with $1

more info on pattern syntax here: http://www.php.net/manual/en/reference.pcre.pattern.syntax.php
0
 
LVL 2

Assisted Solution

by:Andy_Neodynium
Andy_Neodynium earned 75 total points
ID: 11911440
How about :

$text = preg_replace('/\[([bui]):[0-9a-f]+\\](.*)\[\/([bui]):[0-9a-f]+\]/Usi', '<$1>$2</$3>', $announcement);

For all your bold, underline and italic needs in one go.

Note that I only use one slash to escape things like the square brackets (\[ not \\[)
I suspect that the \\ is escaping the slash, and then the square bracket is being treated like a class.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 2

Expert Comment

by:Andy_Neodynium
ID: 11911443
How I tested it:

<?
$text = "[i:33cc0ee790]Paranoia[/i:33cc0ee790]";

$text = preg_replace('/\[([bui]):[0-9a-f]+\\](.*)\[\/([bui]):[0-9a-f]+\]/Usi', '<$1>$2</$3>', $text);

//             $1 is the same as //1, but preferred according to the manual

echo $text;
?>
0
 
LVL 10

Author Comment

by:rockmansattic
ID: 11912045
Thank you both,
Diablo84 did answer my question, So thats why you got most pts.  Thanks
Also, Andy_Neodynium 's part about [bui] is new to me and I will use this piece of code.

Thank you.
rockman
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 11912070
no problem :)

|)iablo
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now