Viewed   37 times

Can anyone help me with a solution that pulls the position and value of a random character from a given string using PHP. For example I have a a string variable $string = 'helloworld'; and would like to randomly select a character from $string and echo the character and its position.


$str = 'helloworld';

$randomChar = $str[rand(0, strlen($str)-1)];


Saturday, November 12, 2022

Here's what worked best for me when trying to script this (in case anyone else comes across this like I did):

$ pecl -d php_suffix=5.6 install <package>
$ pecl uninstall -r <package>

$ pecl -d php_suffix=7.0 install <package>
$ pecl uninstall -r <package>

$ pecl -d php_suffix=7.1 install <package>
$ pecl uninstall -r <package>

The -d php_suffix=<version> piece allows you to set config values at run time vs pre-setting them with pecl config-set. The uninstall -r bit does not actually uninstall it (from the docs):

vagrant@homestead:~$ pecl help uninstall
pecl uninstall [options] [channel/]<package> ...
Uninstalls one or more PEAR packages.  More than one package may be
specified at once.  Prefix with channel name to uninstall from a
channel not in your default channel (

  -r, --register-only
        do not remove files, only register the packages as not installed

The uninstall line is necessary otherwise installing it will remove any previously installed version, even if it was for a different PHP version (ex: Installing an extension for PHP 7.0 would remove the 5.6 version if the package was still registered as installed).

Monday, December 12, 2022

Use random.sample

numbers = random.sample(xrange(1000, 10000), 100)  # or `range` in Python 3

The sorting part is easy - use the list.sort method.


By default this will sort it from smallest number to largest, but it takes an optional key argument which determines what to sort it on.

There is also a sorted function which doesn't modify a list in-place, but rather returns a sorted list.

numbers_sorted = sorted(numbers)

This also has an optional key argument.

Thursday, September 8, 2022

You could simply do:

myString = myString.Replace(" ", "");

If you want to remove all white space characters you could use Linq, even if the syntax is not very appealing for this use case:

myString = new string(myString.Where(c => !char.IsWhiteSpace(c)).ToArray());
Sunday, October 23, 2022

Never used any of those, but they look interesting..

Take a look at Gearman as well.. more overhead in systems like these but you get other cool stuff :) Guess it depends on your needs ..

Friday, November 11, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :