Solved

Explain this preg_replace

Posted on 2004-08-26
6
462 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to dynamically set the form action using jQuery.
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.

737 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