trouble getting random item from array to display

jlknauff

New member
Aug 25, 2008
237
1
0
Here is what I currently have:

$spin = get_string_between($body, "{{{", "}}}");
$spin = explode("|", $spin);
shuffle($spin);
$spun = $spin[0];
$body = str_replace("{{{", "$spun", $body);
$body = str_replace("}}}", "", $body);
echo $body;

but rather than placing a random item from the array into $body, it places the whole array with the | still in it.

If I do print_r($spin[0]) it displays one random item from the array the way it should.

Does this make any sense? What am I doing wrong?
 


Ahhh...can't beleive I missed that :)

ok...still having a problem. I'm getting it to generate a random item from the array, but it also prints out the whole array immediately after it.

$body = "Some paragraph {{{goes|was|went}}} here.";
$spin = get_string_between($body, "{{{", "}}}");
$spin = explode("|", $spin);
shuffle($spin);
$spun = $spin[0];
$body = str_replace("{{{", $spin[0], $body);
$body = str_replace("}}}", "", $body);
echo $body;

It produces this:

Some paragraph goesgoes|was|went here.
 
Try:
$body = "Some paragraph {{{goes|was|went}}} here.";
$target = get_string_between($body, "{{{", "}}}");
$spin = explode("|", $target);
shuffle($spin);
$spun = $spin[0];
$body = str_replace("{{{".$target."}}}", $spun, $body);
echo $body;
 
I've managed to get it to work for the most part, but it was only spinning the first set. I'm trying to use an if else statement so that it will spin all instances, but it doesnt output anything thie way. Any idea what I'm doing wrong here?

Code:
$body = "Some paragraph {{{goes|was|went}}} {{{there|somewhere|here}}}.";
if (strstr($body, "{{{")){
 $target = get_string_between($body, "{{{", "}}}");
 $spin = explode("|", $target);
 shuffle($spin);
 $spun = $spin[0];
 $body = str_replace("{{{".$target."}}}", $spun, $body);
}
else
 echo $body;
 
You have to start thinking about how to find a set of matches. In other words, you have to change the way you're looking at the problem. The way I solved it(in the thread linked from above) was by applying a preg_replace_callback callback on the text in a loop, and saving the new text, until the text stopped changing.

Note: In that code I posted a while ago, the pregcallback argument should be quoted(I had probably recently done some JS where functions aren't treated like ugly bitches like they are in PHP), and strcmp'ing the text probably isn't necessary(probably doesn't need to be binary safe).
 
Code:
function spinningDicks( $string ) {
	preg_match_all( "/{{{(.*?)}}}/", $string, $matches );
	if ( isset( $matches[1] ) ) {
		foreach( $matches[1] as $spinMatch ) {
			$parts = explode( "|", $spinMatch );
			shuffle( $parts );
			$string = str_replace( "{{{".$spinMatch."}}}", $parts[0], $string );
		}
	}
	return $string;
}

echo spinningDicks( "Some paragraph {{{goes|was|went}}} here." );

Kind of shocked no one posted anything yet, it's quite a simple problem O_o
 
  • Like
Reactions: stumpyb
Kineticbug, that worked perfectly. Would it be very difficult to make that work with nested spins, such as:

Some text {{{{{{was|used to be}}} in here|{{{goes|belongs}}} over there}}}
 
Kineticbug, that worked perfectly. Would it be very difficult to make that work with nested spins, such as:

Some text {{{{{{was|used to be}}} in here|{{{goes|belongs}}} over there}}}

It'd probably be easier to use alternating delimiters for each level since depending on how you code it, you might mistakenly grab the first }}} instead of the last.
 
Code:
function spinningDicks( $string ) {
	preg_match_all( "/{{{(.*?)}}}/", $string, $matches );
	if ( isset( $matches[1] ) ) {
		foreach( $matches[1] as $spinMatch ) {
			$originalSpinMatch = $spinMatch;
			preg_match_all( "/\(\(\((.*?)\)\)\)/", $spinMatch, $subMatches );
			if ( isset( $subMatches[1] ) ) {
				foreach( $subMatches[1] as $subSpinMatch ) {
					$subParts = explode( "|", $subSpinMatch );
					shuffle( $subParts );
					$spinMatch = str_replace( "(((".$subSpinMatch.")))", $subParts[0], $spinMatch );
				}
			}
			$parts = explode( "|", $spinMatch );
			shuffle( $parts );
			$string = str_replace( "{{{".$originalSpinMatch."}}}", $parts[0], $string );
		}
	}
	return $string;
}

echo spinningDicks( "{{{(((was|used to be))) in here|(((goes|belongs))) over there}}}" );

{{{ for the first level delimiter, ((( for the second.
The script is a bit of a cluster fuck and would work a bit better if it were an object, but, there you go :P
 
Odd, we can't edit posts after a certain time in this section?
Anyway, revised a bit:

Code:
function spinText( $string ) {
	$parts = explode( '|', $string );
	return $parts[ array_rand( $parts ) ];
}

function spinningDicks( $string ) {
	if ( preg_match_all( "/{{{(.*?)}}}/", $string, $matches ) != 0 ) {
		foreach( $matches[1] as $spinMatch ) {
			$originalSpinMatch = $spinMatch;
			if ( preg_match_all( "/\(\(\((.*?)\)\)\)/", $spinMatch, $subMatches ) != 0 ) {
				foreach( $subMatches[1] as $subSpinMatch ) {
					$spinMatch = str_replace( "(((".$subSpinMatch.")))", spinText( $subSpinMatch ), $spinMatch );
				}
			}
			$string = str_replace( "{{{".$originalSpinMatch."}}}", spinText( $spinMatch ), $string );
		}
	}
	return $string;
}

echo spinningDicks( "spinning dicks {{{(((were|used to be))) in here|(((go|belong))) in there}}}" );
 
You guys should stop calling yourselves coders.

Code:
$spin_this = 'Some text {{{{{{was|used to be}}} in here|{{{goes|belongs}}} over there}}}';

function spin( $string ) {

	while ( preg_match( '/{{{([^{}]+)}}}/', $string, $matches ) ) {
		$pcs = explode( '|', $matches[1]);
		$string = str_replace($matches[0], $pcs[array_rand($pcs)], $string);
	}

	return $string;

}

for ($i=0;$i<10;$i++) {
	echo spin($spin_this),"\n";
}
 
Check the link I posted above for code that works with nested spins.
 
Code:
$spinStr = 'Some text {{was|{used to be|{may|might} have been}} in here|{goes|belongs} over there}';
sub{while($i++<$_[1]){my $s=$_[0];while($s=~/{([^{}]+)}/ and @r=split(/\|/, $1) and $s=~s/{\Q$1\E}/$r[rand @r]/){};$_[2]->($s);}}->($spinStr,10,sub{print $_[0]."\n";})
The lack of white space fact that it's perl, I believe, makes my e-penis even huger than your guys'. Not to mention the completely gratuitous use of anonymous subs.
Oh yeah and it doesn't matter if it outputs a non-unique set since thats not really what spinning's all about anyway.
 
Code:
$spinStr = 'Some text {{was|{used to be|{may|might} have been}} in here|{goes|belongs} over there}';
sub{while($i++<$_[1]){my $s=$_[0];while($s=~/{([^{}]+)}/ and @r=split(/\|/, $1) and $s=~s/{\Q$1\E}/$r[rand @r]/){};$_[2]->($s);}}->($spinStr,10,sub{print $_[0]."\n";})
The lack of white space fact that it's perl, I believe, makes my e-penis even huger than your guys'. Not to mention the completely gratuitous use of anonymous subs.

Real Perl programmers use strict.

Code:
use strict;
use warnings;

my $spinStr = 'Some text {{was|{used to be|{may|might} have been}} in here|{goes|belongs} over there}';

sub{my($i,@r)=$_[1];while($i--){$_=$_[0];1 while(/{([^{}]+)}/ and @r=split(/\|/,$1) and s/{\Q$1\E}/$r[rand @r]/);$_[2]->($_)}}->($spinStr,10,sub{print $_[0],"\n"});

Fixed. Even shaved off a few characters from your "one liner" to show how HUUUUUUUUuuuge my Perl e-peen is.

Oh yeah and it doesn't matter if it outputs a non-unique set since thats not really what spinning's all about anyway.

Who said anything about unique? This thread was about getting random elements from and array. If you want guaranteed unique, either don't use random elements or use a seperate function (or method, or subroutine, or what-the-fuck-ever) to check the spun text against all other previously spun text.
 
You guys should stop calling yourselves coders

This


Here's my entry for the biggest Perl e-peen contest.

Code:
use strict;
use warnings;

my $spinStr = 'Some text {{was|{used to be|{may|might} have been}} in here|{goes|belongs} over there}';

sub{for(my($t,$i,%h,@r)=$_[1];;){$_=$_[0];1 while(/{([^{}]+)}/ and @r=split(/\|/,$1) and s/{\Q$1\E}/$r[rand @r]/);!exists $h{$_}&&$_[2]->($_)&&$h{$_}++;last if (keys %h)==$t || ++$i>$t*100;}}->($spinStr,10,sub{print $_[0],"\n"});

It'll weed out the dupes and will try to return the requested number of spins. Performance guaranteed to degrade as the length of $spinStr increases.

========D - - - -
OO