I've written my very first PHP application but I'm stuck on one thing! This is what I've got <?php $quote = $_POST['quote']; ?> PHP: <form action="index.php" method="post"> <textarea rows="10" cols="30" name="quote" id="quote"></textarea> <input type="submit" value="Submit"> </form> HTML: Now, say someone throws this into the form: I'd need it to strip the spaces out, and between the letters in the alphabet spit it out like this <a>hello</a><b>how</b><c>are</c> BUT the letters are to be random not in order so it can be <b>hello</b><z>how</z><c>are</c> etc. Is this possible?
Hello, Take a look around this functions http://ar2.php.net/manual/es/function.explode.php Maybe this can help you. Best, Jakomo
Hmm, the only thing I foud relevant is this: <?php function between($beg, $end, $str) { $a = explode($beg, $str, 2); $b = explode($end, $a[1]); return $beg . $b[0] . $end; } echo between('<a>', '</a>', 'fsdfsdfsd<a>fsdfsd<a><a></a>sdfsdfsdf') //<a>fsdfsd<a><a></a> ?> PHP: But I don't understan how to get the random <b> </b> <f></f> etc wrapped around each word randomly.
I terribly fail to see what this would be useful for, but I guess it has its purpose. This works: function wrap_word($word) { $letter = chr(rand(97, 122)); return "<{$letter}>{$word}</{$letter}>"; } $string = 'Hello how are you today'; $string = preg_replace('/\s*([\w]+)\s*/e', 'wrap_word("$1")', $string); echo $string; PHP:
Hello, You can do something like this $tag[1]="<b>" $tag[2]="<f>" $tag[3]="<a>" count how many words you have in the string with count Example: Hello happy friends You here have 3 words when make a loop of this three words while (1 to count(string)) { Then use the rand function to get a randon number.. http://ar2.php.net/rand echo tag[rand]. WORLD STRING (ie: Hello). "/".tag[rand]; } Well this is the idea, I hope you can understand it. Jakomo
It's for a dynamic text replacement script im making, so each word is spit out in its own font/color.
Here another way to do it, 'cause I was bored. $string = ' Hello how are you today'; $string = preg_replace_callback( '/\s*([^\s]+)\s*/', create_function( '$word', '$letter = chr(rand(97, 122)); return "<{$letter}>{$word[1]}</{$letter}>";' ), $string ); echo $string; PHP:
The second version should do that. Or just use the RegEx pattern from the second in the first version.