-1

i have been saving buffer content in a mysql text field , but when content exceeded the number of characters limit, code is crunched.

so how can i optimize html buffer content by removing tabs and white spaces from the code using php ?

Sumit Bijvani
  • 8,154
  • 17
  • 50
  • 82
ZMajico
  • 99
  • 1
  • 2
  • 6
  • 2
    Use a field type that can store more characters? – N.B. Apr 12 '12 at 13:17
  • 3
    What problem are you trying to solve? Why are you even storing HTML in your database to begin with? – kba Apr 12 '12 at 13:19
  • I'm with Kristian here, it's best to not store compiled HTML in a database. It gets messy, and you're just running into one of the problems now. – MetalFrog Apr 12 '12 at 13:21

3 Answers3

0

You could use a regular expression like:

preg_replace("/\s+/", " ", $string);

That should replace all multiple white-spaces, tabs and new-lines with just one.

See also: How do I remove extra spaces, tabs and line feeds from a sentence and substitute them with just one space?

Community
  • 1
  • 1
tonymarschall
  • 3,862
  • 3
  • 29
  • 52
0

You could compress the data with a function such as gzdeflate. See http://php.net/manual/en/function.gzdeflate.php

If this needs to go into a non-binary safe location, you could base64_encode the gzipped string, and store afterwards.

pp19dd
  • 3,625
  • 2
  • 16
  • 21
0

Why don't you use a type able to keep all your data? E.g. LONGTEXT or MEDIUMTEXT? You can however strip away all repetition of space character using

preg_replace("/\s+/", " ", trim($s));

ADD

As noticed in comments, this will crunch every repeated "space" character even when it's meaningful. This won't work with default pre tag style, and with any other tag with CSS that changes the white-space attribute.

ShinTakezou
  • 9,432
  • 1
  • 29
  • 39