Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regular Expressions or no?

Status
Not open for further replies.

Hameedullah

Programmer
Aug 12, 2002
29
0
0
AU
I want to find any newline "\n" and replace it with <BR> only if it does not follow by certain HTML tags (eg <p>, <br>, etc) that would double up the spaces.

Can anyone help me what is the best way to go about this? Should i use regex's? if so then can you tell me how to write the pattern?

Cheers,
Hameed
 
Code:
<?php
echo [b]nl2br[/b]("foo isn't\n bar");
?>

nl2br doesn't replace the newlines, but because a browser just treats newlines as whitespace this should do what you're looking for

--Paul


cigless ...
 
>>nl2br doesn't replace the newlines

i am confused at what u r trying to say.

this is from the manual:
>>Returns string with '<br />' inserted before all newlines.


Hameed try tihs:
$TheStr=preg_replace("/\n(<br>|<p>)/i","$1",$TheStr);
$TheStr=nl2br($TheStr);

to add more tags just add a | and add the tag. e.g. if u want to add <hr> to it then:
$TheStr=preg_replace("/\n(<br>|<p>|<hr>)/i","$1",$TheStr);


Known is handfull, Unknown is worldfull
 
vbkris said:
i am confused at what u r trying to say.
Hameed's requirement is to replace \n with <br>, I'm suggesting that the nl2br function is what he needs, because the browser will ignore the newlines anyway, and just insert <br /> beside them.

the <br /> is understood by browsers as <BR>, but is XHTML compliant. I appreciate that what I posted isn't what the OP requested, but might be a better solution for his requirements.

Not to trying to tick anyone off, if the post causes offence, feel free to RF it

Regards
--Paul

cigless ...
 
>>Not to trying to tick anyone off, if the post causes offence, feel free to RF it

now what made u think that???


since hameed says:
>>I want to find any newline "\n" and replace it with <BR> only if it does not follow by certain HTML tags

and u say:
>>I appreciate that what I posted isn't what the OP requested, but might be a better solution for his requirements

i guess thats it.
everybody here is only posting with the aim of helping. i understand that too...

Known is handfull, Unknown is worldfull
 
Thanks guys.
Paul, I guess my question was not clear enough. Sorry for misunderstanding.

vbKris, I will try the solution. Thanks

Hameed

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top