Solved

Explain this preg_replace

Posted on 2004-08-26
6
461 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …

685 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