PDA

View Full Version : preg_replace help


Gregor
Fri 7th Jun '02, 5:02am
i'we got this code:

$text = preg_replace ("#(.*?)[^>]\r\n#si", "\\1<br />\n", $text);


1. i include the file and grab it into $text var,
2. this file if in HTML format, but without < br / > tags!
3. so i used this function, that adds < br / > tag in every line which DOES NOT end with ">" (html end tag symbol)

it does what needed with one mistake. in lines, that doesn't end with ">", it removes me THE LAST char (., [a-z]...)


what is wrong?

please help,

Thanks and best regards

Mark Hensler
Fri 7th Jun '02, 1:45pm
not tested...


$text = preg_replace ("#(.*?)[^>]$#i", "\\\\1<br />\n", $text);
You don't want to use the 's' modifier. Try using the $ anchor instead of hardcoding \r\n.

Gregor
Fri 7th Jun '02, 6:06pm
Originally posted by Mark Hensler
not tested...


$text = preg_replace ("#(.*?)[^>]$#i", "\\\\1<br />\n", $text);
You don't want to use the 's' modifier. Try using the $ anchor instead of hardcoding \r\n.

using this way, it doesn't do anything..

but if i use just "\n" and not $ or "\r\n" it puts < br / > brackets to all lines :'(!!!

help :s

Chen
Sat 8th Jun '02, 3:22am
This works:
$text = preg_replace ('#(.*?[^>])(\r)?\n#', "\$1<br />\n", $text);
You also don't want the 'i' modifier, you are not matching any letters. Why do people always put that in... ;)

Gregor
Sat 8th Jun '02, 5:10am
um...

it's like this,.. maybe you didn't understand me well. i have a file, in which i am also using html tags (WITHOUT < br > TAG).

if some line looks like this: "blahblah and more blah.", the "< br >" should be created after this line

if some line looks like this: "and you cannot < i >download it < /i >"
it WONT add the < br > tag at the end, which this snippet does. um...

i don't know what is the problem, but i'm begging you to help me again...

thank you very much

Chen
Sat 8th Jun '02, 5:20am
Originally posted by Gregor
3. so i used this function, that adds < br / > tag in every line which DOES NOT end with ">" (html end tag symbol)

it does what needed with one mistake. in lines, that doesn't end with ">", it removes me THE LAST char (., [a-z]...)
The code I gave you above does exactly what you wanted. It only adds <br /> after lines that do not end with >. And it does not remove the last character.

Gregor
Sat 8th Jun '02, 5:27am
Specifications when writing this article were:

<ul>
<li>System <i>Windows XP Professional</i> (<a href="" target="_blank">link</a>)</li>
<li>Server <i>Internet Information Services (IIS) version 5.1</i> (<a href="" target="_blank">link</a>)</li>
<li>PHP 4.2.0 (<a href="" target="_blank">link</a>)</li>
<li>MySQL 3.23.39 (<a href="" target="_blank">link</a>)</li>
</ul>


returns:


Specifications when writing this article were<br />
<br />
<ul><br />
<li>System <i>Windows XP Professional</i> (<a href="" target="_blank">link</a>)</li><br />
<li>Server <i>Internet Information Services (IIS) version 5.1</i> (<a href="" target="_blank">link</a>)</li><br />
<li>PHP 4.2.0 (<a href="" target="_blank">link</a>)</li><br />
<li>MySQL 3.23.39 (<a href="" target="_blank">link</a>)</li><br />
</ul><br />



it should be:


Specifications when writing this article were:<br />
<br />
<ul>
<li>System <i>Windows XP Professional</i> (<a href="" target="_blank">link</a>)</li>
<li>Server <i>Internet Information Services (IIS) version 5.1</i> (<a href="" target="_blank">link</a>)</li>
<li>PHP 4.2.0 (<a href="" target="_blank">link</a>)</li>
<li>MySQL 3.23.39 (<a href="" target="_blank">link</a>)</li>
</ul>