Solved

Explain this preg_replace

Posted on 2004-08-26
6
459 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
xampp tool 12 48
curl SSL certificate unable to get local issuer certificate 15 65
Wordpress errors showing up, hacked? 4 33
hasing a url 16 24
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

776 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