September 1st, 2000, 09:03 PM
-
Hi, i am accepting a string input from a textarea. My problem is that if a user sticks in a long unbroken string such as "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" my entire page will try to fit it. I need to be able to check within a string to see if any of the words within it are larger than a certain number of chars, and if it is break it at the max point.
Hence if someone enters: "I like to addlotsofcharacterstogethertobeaproblematicchild but I am still friendly" the large word would be broken, and a " " would be added.
Thanks!
September 2nd, 2000, 04:41 PM
-
You can get the length of a string by doing a sizeof. Example:
$string = "blahblahblahblahblahblah";
$length = sizeof($string);
Hope that helps =)
September 2nd, 2000, 06:52 PM
-
If you want to analyze each word within a string then you will probably want to explode the string and loop through it or better yet, use a regexp with eregi_replace(). I've illustrated the former for you below:
<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
$maxcharacters = 15; // change to maximum number of characters to accept
$string = explode(" ", $full_string);
$sizeof_string = count($string);
for($i=0; $i < $sizeof_string; $i++) {
if(strlen($string[$i]) > $maxcharacters) {
$string[$i] = " ";
}
}
[/code]
------------------
<UL TYPE=SQUARE>
<LI> TD Scripts
<LI> Script School
<LI>php-scripts
</UL>
[This message has been edited by TDavid (edited September 02, 2000).]