I have code generating a random font and applying a new random font to each line of text and would like to add a
$line = str_replace ("a", "@", $line);
But I would like the chance of this being applied to each line to be 10% rather than applied to the string as a whole. How can i do it? Here’s my existing code:
$fonts = array("Helvetica", "Arial", "Courier", "Georgia", "Serif", "Comic Sans", "Tahoma", "Geneva", "Times New Roman");
shuffle($fonts);
$output = "";
$lines = array_slice(file("users.txt"), -20, 20);
$i = 0;
foreach ( $lines as $line ) {
if($i == count($fonts)) {
shuffle($fonts);
$i = 0;
}
$output .= '<div style="font-size: ' . rand(15, 23) . 'px; font-family:' . $fonts[$i] . '; margin-left: ' . rand(0, 60) . '%; opacity: 0.8;">' . $line . "</div>\n";
$i++;
}
echo $output;
I’m just starting to get familiar with php, I feel as though I’ve got a whole new web to play with! 😀
If you’d like to have 10% chance of having your ‘a’ changed with ‘@’ then try inserting this code:
It generates an Integer from 0 – 9 and if it equals 1 (10% chance, then it applies your formatting.