944,117 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 1139
  • PHP RSS
Oct 11th, 2007
0

Regular expressions and formatting

Expand Post »
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:

PHP Syntax (Toggle Plain Text)
  1. $bad = array("<",">","&lt;","&gt;");
  2. $good = array("[","]","[","]");
  3.  
  4. 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!
Similar Threads
Reputation Points: 12
Solved Threads: 0
Newbie Poster
jglw is offline Offline
24 posts
since Apr 2006
Oct 14th, 2007
0

Re: Regular expressions and formatting

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 14th, 2007 at 12:13 am.
Reputation Points: 12
Solved Threads: 0
Newbie Poster
jglw is offline Offline
24 posts
since Apr 2006
Oct 14th, 2007
0

Re: Regular expressions and formatting

> 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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Oct 14th, 2007
0

Re: Regular expressions and formatting

Changed it to
PHP Syntax (Toggle Plain Text)
  1. 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
PHP Syntax (Toggle Plain Text)
  1. if($iterator % 80 == 0) {echo "<br />"}
Reputation Points: 12
Solved Threads: 0
Newbie Poster
jglw is offline Offline
24 posts
since Apr 2006
Oct 14th, 2007
0

Re: Regular expressions and formatting

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.
Reputation Points: 235
Solved Threads: 193
Nearly a Posting Virtuoso
kkeith29 is offline Offline
1,315 posts
since Jun 2007
Oct 15th, 2007
0

Re: Regular expressions and formatting

PHP Syntax (Toggle Plain Text)
  1. $qry = "SELECT Post FROM POST WHERE ID = " . $_GET['threadID'];
  2. $rset = mysql_query($qry);
  3. $row = mysql_fetch_array($rset);
  4. 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:
PHP Syntax (Toggle Plain Text)
  1. for($i=0;$i<strlen($row[0]);$i+=80){
  2. echo substr($row[0],$i,80);
  3. echo "\n";
  4. }

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.
Reputation Points: 12
Solved Threads: 0
Newbie Poster
jglw is offline Offline
24 posts
since Apr 2006
Oct 15th, 2007
0

Re: Regular expressions and formatting

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.
Reputation Points: 12
Solved Threads: 0
Newbie Poster
jglw is offline Offline
24 posts
since Apr 2006
Oct 15th, 2007
0

Re: Regular expressions and formatting

Also, you'd need something like:
PHP Syntax (Toggle Plain Text)
  1. if (instr("<br />", substr($row[0],$i,80))){
  2. $i+= 80-(instr("<br />",substr($row[0],$i,80))+5);
  3. }
In that fix I put up to stop you wrapping half way through a line for which the previous line had a break.
Reputation Points: 12
Solved Threads: 0
Newbie Poster
jglw is offline Offline
24 posts
since Apr 2006
Oct 15th, 2007
0

Re: Regular expressions and formatting

But yeah, I haven't tested any of this, it's just food for thought.
Reputation Points: 12
Solved Threads: 0
Newbie Poster
jglw is offline Offline
24 posts
since Apr 2006
Oct 15th, 2007
0

Re: Regular expressions and formatting

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
Reputation Points: 235
Solved Threads: 193
Nearly a Posting Virtuoso
kkeith29 is offline Offline
1,315 posts
since Jun 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: session variable error
Next Thread in PHP Forum Timeline: Simple article manager script in PHP I wrote





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC