User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 427,861 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,599 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 659 | Replies: 9
Reply
Join Date: Apr 2006
Posts: 24
Reputation: jglw is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
jglw jglw is offline Offline
Newbie Poster

Regular expressions and formatting

  #1  
Oct 11th, 2007
I'm trying to get some post data formatted for output on a browser and I can't use pre tags becuase the width attribute isn't supported anymore and it doesn't really work. I also need to break up a word if it's over 80 characters long, if someone's been a pillock basically and posted some crap. However, if you could advise on that it would be great, but the real problem is I can't seem to replace the newlines with brs. Here's the code:

$bad  = array("<",">","&lt;","&gt;");
$good = array("[","]","[","]");

preg_replace( "/[\r\n]+/" , "<br />" , str_replace( $bad , $good , mysql_real_escape_string( $_POST['post'] ) ) )

Any help?
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
LOL it looks like the owner of this site needs some help with the formatting too!
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2006
Posts: 24
Reputation: jglw is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
jglw jglw is offline Offline
Newbie Poster

Re: Regular expressions and formatting

  #2  
Oct 13th, 2007
Where's my off the cuff insult for being so stupid and one letter solution? Is this cos I outed a measly formatting error with the site. Come on!!!!
Last edited by jglw : Oct 13th, 2007 at 11:13 pm.
Reply With Quote  
Join Date: Aug 2005
Posts: 4,782
Reputation: iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light 
Rep Power: 17
Solved Threads: 319
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: Regular expressions and formatting

  #3  
Oct 14th, 2007
> but the real problem is I can't seem to replace the newlines with brs.

Honey why ain't you using nl2br();

>I also need to break up a word if it's over 80 characters long

Find out what the max no. of letters you can have per line. Then perform tricks using the modulus operator. You understand that honey?

>LOL it looks like the owner of this site needs some help with the formatting too!

She's too lazy to fix that.
I'm not a programmer. My attitude starts with ignorance, holds steady at conversation, and ends with a trip to the hospital. Get used to it.
Reply With Quote  
Join Date: Apr 2006
Posts: 24
Reputation: jglw is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
jglw jglw is offline Offline
Newbie Poster

Re: Regular expressions and formatting

  #4  
Oct 14th, 2007
Changed it to
nl2br(str_replace($bad,$good,mysql_real_escape_string($_POST['Post'])))
but still no <br /> tags are ending up in the database. The POST is coming from a textarea, if this helps...

Also, the modulus thing, no I don't get. Is there a way to print a string one letter at a time in a for loop and you go
if($iterator % 80 == 0) {echo "<br />"}
Reply With Quote  
Join Date: Jun 2007
Location: Valley Center, Kansas
Posts: 554
Reputation: kkeith29 is on a distinguished road 
Rep Power: 3
Solved Threads: 57
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Posting Pro

Re: Regular expressions and formatting

  #5  
Oct 14th, 2007
i just made a code to break up the words for you. you will need to customize it to work with you setup. i can do this for you but you need to post all of your code so i can see exactly what you are doing.
Reply With Quote  
Join Date: Apr 2006
Posts: 24
Reputation: jglw is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
jglw jglw is offline Offline
Newbie Poster

Re: Regular expressions and formatting

  #6  
Oct 15th, 2007
$qry = "SELECT Post FROM POST WHERE ID = " . $_GET['threadID'];
$rset = mysql_query($qry);
$row = mysql_fetch_array($rset);
echo $row[0];

I haven't really looked at the problem with a word being too long yet. I assume the below is the fix:
for($i=0;$i<strlen($row[0]);$i+=80){
echo substr($row[0],$i,80);
echo "\n";
}

However, it's problem that passing the post from a textarea to nl2br() isn't adding the br tags that's really getting me depressed.
Reply With Quote  
Join Date: Apr 2006
Posts: 24
Reputation: jglw is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
jglw jglw is offline Offline
Newbie Poster

Re: Regular expressions and formatting

  #7  
Oct 15th, 2007
I read this on a forum somewhere:

Either your clean_var or mysql_less_safe functions are probably escaping the \r and \n characters using addslashes or mysql_real_escape_string. If that's the case, you'd need to do a str_replace for "\\\r\\\n" (or something like that I've never really learned how to count up all the slashes -- echo out the $new_course_poc to see what they are being turned into)

I guess this means that you just need to do the nl2br() after the tags are removed and before the mysql_escape_string() is called. I'm at work at the moment so I can't test my theory, but maybe is the solution.
Reply With Quote  
Join Date: Apr 2006
Posts: 24
Reputation: jglw is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
jglw jglw is offline Offline
Newbie Poster

Re: Regular expressions and formatting

  #8  
Oct 15th, 2007
Also, you'd need something like:
if (instr("<br />", substr($row[0],$i,80))){
$i+= 80-(instr("<br />",substr($row[0],$i,80))+5);
}
In that fix I put up to stop you wrapping half way through a line for which the previous line had a break.
Reply With Quote  
Join Date: Apr 2006
Posts: 24
Reputation: jglw is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
jglw jglw is offline Offline
Newbie Poster

Re: Regular expressions and formatting

  #9  
Oct 15th, 2007
But yeah, I haven't tested any of this, it's just food for thought.
Reply With Quote  
Join Date: Jun 2007
Location: Valley Center, Kansas
Posts: 554
Reputation: kkeith29 is on a distinguished road 
Rep Power: 3
Solved Threads: 57
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Posting Pro

Re: Regular expressions and formatting

  #10  
Oct 15th, 2007
so if am reading this right, you want to add <br /> before it goes into the database. from the code you wrote it is adding <br /> when its being retrieved from the database
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb PHP Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 3:41 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC