| | |
Regular expressions and formatting
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Apr 2006
Posts: 24
Reputation:
Solved Threads: 0
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:
Any help?
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
LOL it looks like the owner of this site needs some help with the formatting too!
PHP Syntax (Toggle Plain Text)
$bad = array("<",">","<",">"); $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!
> 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.
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.
*Voted best profile in the world*
•
•
Join Date: Apr 2006
Posts: 24
Reputation:
Solved Threads: 0
Changed it to
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)
nl2br(str_replace($bad,$good,mysql_real_escape_string($_POST['Post'])))
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)
if($iterator % 80 == 0) {echo "<br />"}
•
•
Join Date: Apr 2006
Posts: 24
Reputation:
Solved Threads: 0
PHP Syntax (Toggle Plain Text)
$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:
PHP Syntax (Toggle Plain Text)
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.
•
•
Join Date: Apr 2006
Posts: 24
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Apr 2006
Posts: 24
Reputation:
Solved Threads: 0
Also, you'd need something like:
In that fix I put up to stop you wrapping half way through a line for which the previous line had a break.
PHP Syntax (Toggle Plain Text)
if (instr("<br />", substr($row[0],$i,80))){ $i+= 80-(instr("<br />",substr($row[0],$i,80))+5); }
![]() |
Similar Threads
- Regular Expressions (VB.NET)
- Parsing a log file using regular expressions (C#)
- Regular Expressions (C#)
- Regular Expressions (Computer Science)
- Regular Expressions (C#)
- Regular Exp (Python)
- mod_rewrite: help with regular expressions (Linux Servers and Apache)
- matching regular expressions (Java)
- Zend PHP Certification (PHP)
Other Threads in the PHP Forum
- Previous Thread: session variable error
- Next Thread: Simple article manager script in PHP I wrote
| Thread Tools | Search this Thread |
apache api array basic beginner binary broken cache cakephp checkbox class cms code computing confirm cron curl customizableitems database date delete display dynamic echo email error external file files filter folder form forms forum function functions gc_maxlifetime google headmethod host howtowriteathesis href htaccess html iframe image include insert ip javascript joomla limit link login mail malfunction memmory memory menu mlm multiple mysql navigation oop parsing paypal pdf php phpmysql problem query question radio random recursion remote script search select server sessions sms snippet source space sql syntax system table thesishelp trouble tutorial update upload url validator variable video web youtube






