Another PHP Question =)

Discussion in 'PHP' started by JosS, Aug 20, 2007.

  1. #1
    I've written my very first PHP application but I'm stuck on one thing! :eek:

    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?
     
    JosS, Aug 20, 2007 IP
  2. jakomo

    jakomo Well-Known Member

    Messages:
    4,262
    Likes Received:
    82
    Best Answers:
    0
    Trophy Points:
    138
    #2
    jakomo, Aug 20, 2007 IP
  3. JosS

    JosS Guest

    Messages:
    369
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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.
     
    JosS, Aug 20, 2007 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    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:
     
    nico_swd, Aug 20, 2007 IP
  5. jakomo

    jakomo Well-Known Member

    Messages:
    4,262
    Likes Received:
    82
    Best Answers:
    0
    Trophy Points:
    138
    #5
    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
     
    jakomo, Aug 20, 2007 IP
  6. JosS

    JosS Guest

    Messages:
    369
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #6
    It's for a dynamic text replacement script im making, so each word is spit out in its own font/color.
     
    JosS, Aug 20, 2007 IP
  7. JosS

    JosS Guest

    Messages:
    369
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thanks nico_swd, you've come through again!

    Cheers!
     
    JosS, Aug 20, 2007 IP
  8. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #8
    Here another way to do it, 'cause I was bored. :p

    
    $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:
     
    nico_swd, Aug 20, 2007 IP
  9. JosS

    JosS Guest

    Messages:
    369
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Is there anyway you can have it so it keeps the "," " ' ", "!" etc in the submitted string
     
    JosS, Aug 20, 2007 IP
  10. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #10
    The second version should do that. Or just use the RegEx pattern from the second in the first version.
     
    nico_swd, Aug 20, 2007 IP