Solved

Explain this preg_replace

Posted on 2004-08-26
6
456 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
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 create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

895 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

13 Experts available now in Live!

Get 1:1 Help Now