PDA

View Full Version : Help with preg_replace


JulianD
04-17-2004, 07:17 PM
Hi.

I'm a completely noob when it comes to regexp, so I guess I can post here for a little help :p

I need to replace all the <br /> on a variable to simple line breaks (\n). The problem is that I need to replace the <br />'s that are ONLY inside a special tag, for example, the <myhtml> and </myhtml> tags.

See:


<html>
<head><title></title>
</head>
<body>
.
.
.
<myhtml>
Here is some text<br />
and some more text<br />
Bye.
</myhtml>
.
.
.
</body></html>


So, what I want is to convert all the <br />'s inside the <myhtml> tags to simple line breaks.

Is that possible? Please help!!

filburt1
04-17-2004, 07:43 PM
$foo = preg_replace("/<myhtml>(.*)<\/myhtml>/siU", "\\1", $foo);
$foo = str_replace("<br />", "\n", $foo);

Boofo
04-17-2004, 07:46 PM
How would you do that with vbcode tags like the php tags?

JulianD
04-17-2004, 10:32 PM
Hi filburt1. Thanks for your code, but it doesn't work as it should :(

It replaces absolutely all the <br />'s on the variable... But I only need to replace the <br />'s that are inside the <myhtml> and </myhtml> tags.

For example:

This:

<html>
<head><title></title>
</head>
<body>
.
.
Text out side<br />
of the myhtml tags<br />
.
<myhtml>
Here is some text<br />
and some more text<br />
Bye.
</myhtml>
.
.
.
</body></html>

would turn into this:

<html>
<head><title></title>
</head>
<body>
.
.
Text out side<br />
of the myhtml tags<br />
.
<myhtml>
Here is some text\n
and some more text\n
Bye.
</myhtml>
.
.
.
</body></html>


If you could help me with that one, I will thank you my whole life ;)! :p