I want to swap 2 characters in a string with 2 other ones.
Start string = "`bHello `!how `Qare `%you."
Random string = "1234567890abcdefghijklmnopqrstuvwxyz!£$%^&#"
How do i swap `b `! `Q `% with random ones so it looks something like this
End result string = "`4Hello `^how `$are `#you."
I have tried this so far
I tried so far
$out = "`vHow `!are `#you."
$patterns = array("`1","`J","`2","`3","`4","`5","`6","`7","`!","`$","`%","`^","`&","`)","`~","`#","`#","`q","`e","`y","`t","`p","`j","`k","`l","`M","`x","`v","`m","`Q","`E","`R","`T","`Y","`P","`G","`K","`L","`X","`V");
$pretest = array("`1","`J","`2","`3","`4","`5","`6","`7","`!","`$","`%","`^","`&","`)","`~","`#","`#","`q","`e","`y","`t","`p","`j","`k","`l","`M","`x","`v","`m","`Q","`E","`R","`T","`Y","`P","`G","`K","`L","`X","`V");
$tempstr = $pretest[rand(0, strlen($pretest)-1)];
$substs = "`".$tempstr;
$out = preg_replace($patterns, $substs, $out);
However the end result is
$out = "`%How `%are `%you."
it picks only 1 random and changes them all to that one.
<?php
function randomChar() {
$rand = "1234567890abcdefghijklmnopqrstuvwxyz";
return substr($rand, rand(0, strlen($rand)), 1);
}
echo preg_replace_callback("/`./", 'randomChar', "`bHello `!how `Qare `%you.");
Related
I want to capitalize random letters in a string and echo the complete sentence from hello69.world to HeLlO69.WoRlD. Each time the functions run, it should capitalize random letters and exclude special characters and digits. I've tried this so far but it only selects the first 5 characters in string every time and outputs only Capitalized letters. How to fix this?
<?php
$string = "hellohg.09ui8vkosjbdh";
$selchr = substr($string,0, 5);
$caps = strtoupper($selchr);
echo substr_replace($string, $caps,0);
?>
Let's assume you want to CAPITALIZE 5 letters randomly:
$string = "hellohg.09ui8vkosjbdh";
$characters = str_split($string);
$i = 0;
do{
$random_index = rand(0, count($characters) - 1);
$unique_indices[] = ""; //UNIQUE INDICES
while (in_array($random_index, $unique_indices)) {
$random_index = rand(0, count($characters) - 1);
}
$unique_indices[] = $random_index;
$random_letter = $characters[$random_index];
if(ctype_alpha($random_letter)){//only letters
$characters[$random_index] = strtoupper($random_letter);
$i++;
}
}while($i<5);echo implode('', $characters);
Thanks to #El_Vanja for Note UNIQUE INDICES
I want to add "+" character to a string "donalddukeoflagos" multiple times at random positions.
Donalddukeoflagos will now be don+alddu+ke+ofl+agos
The code below is working very well, but it can only add one character at a time.
<?php
$mailad = "Donalddukeoflagos";
$length = (strlen($mailad) - 1);
$insertposition = rand(1,$length);
echo substr_replace($mailad,"+",$insertposition,0);
?>
You can wrap it in a loop.
$specialCharacterCount = 5;
$mailad = "Donalddukeoflagos";
for ($specialCharacterIndex = 0; $specialCharacterIndex < specialCharacterCount; specialCharacterIndex++) {
$length = (strlen($mailad) - 1);
$insertposition = rand(1,$length);
echo substr_replace($mailad,"+",$insertposition,0);
}
how can I get range for bottom string in php?
M0000001:M0000100
I want result
M0000001
M0000002
M0000003
..
..
..
M0000100
this is what i do
<?php
$string = "M0000001:M0000100";
$explode = explode(":",$string );
$text_one = $explode[0];
$text_two = $explode[1];
$range = range($text_one,$text_two);
print_r($range);
?>
So can anyone help me with this?
This is one of many ways you could do this and this is a little verbose but hopefully it shows you some "steps" to take.
It doesn't check for the 1st number being bigger than the 2nd.
It doesn't check your Range strings start with a "M".
It doesn't have all of the required comments.
Those are things for you to consider and work out...
<?php
$string = "M00000045:M000099";
echo generate_range_from_string($string);
function generate_range_from_string($string) {
// First explode the two strings
$explode = explode(":", $string);
$text_one = $explode[0];
$text_two = $explode[1];
// Remove the Leading Alpha character
$range_one = str_replace('M', '', $text_one);
$range_two = str_replace('M', '', $text_two);
$padding_length = strlen($range_one);
// Build the output string
$output = '';
for ( $index = (int) $range_one; $index <= (int) $range_two; $index ++ ) {
$output .= 'M' . str_pad($index, $padding_length, '0', STR_PAD_LEFT) . '<br>';
}
return $output;
}
The output lists a String in the format you have specified in the question. So this is based solely upon that.
This could undergo a few more revisions to make it more function like, as I'm sure some folks will pick out!
In PHP, how to convert a string containing mixture of random letters and numbers to a string containing serial numbers along with random numbers without changing their position in a string? For example,
$str_a = "1)Apple 5)Ball 3)Cat 8)Dog 4)Egg";
Now, I want to convert this string into
$str_b = "1)Apple 2)Ball 3)Cat 4)Dog 5)Egg";
I want the numbers 1,5,3,8,4 to be 1,2,3,4,5.
This should work for you:
Just use preg_replace_callback() and replace each digit (\d+ => 0-9 as many times as possible) with an incrementing number, .e.g
<?php
$str_a = "1)Apple 5)Ball 3)Cat 8)Dog 4)Egg";
$start = 1;
echo $newStr = preg_replace_callback("/\d+/", function($m)use(&$start){
return $start++;
}, $str_a);
?>
output:
1)Apple 2)Ball 3)Cat 4)Dog 5)Egg
Created Simple looping program:
$str_a = "1)Apple 5)Ball 3)Cat 8)Dog 4)Egg";
function change($str_a) {
$counter = 1;
for($i=0; $i<strlen($str_a); $i++) {
if(is_numeric($str_a[$i])) {
$str_a[$i] = $counter++;
}
}
return $str_a;
}
print(change($str_a))
Output:
1)Apple 2)Ball 3)Cat 4)Dog 5)Egg
You can use preg_split and then concatenate it incrementing by 1. This will work also for numbers consisting of multiple digits:
$str_a = "51)Apple 675)Ball 334)Cat 84)Dog 904)Egg";
$a = preg_split('/\b\d+/', $str_a);
$str_b = '';
for($i=0,$c = count($a);$i<$c;$i++){
if($i){
$str_b .= $i.$a[$i];
}
}
echo $str_b;
Output:
1)Apple 2)Ball 3)Cat 4)Dog 5)Egg
I have a PHP variable that looks a bit like this:
$id = "01922312";
I need to replace the last two or three numbers with another character. How can I go about doing this?
EDIT Sorry for the confusion, basically I have the variable above, and after I'm done processing it I'd like for it to look something like this:
$new = "01922xxx";
Try this:
$new = substr($id, 0, -3) . 'xxx';
Result:
01922xxx
You can use substr_replace to replace a substring.
$id = substr_replace($id, 'xxx', -3);
Reference:
http://php.net/substr-replace
function replaceCharsInNumber($num, $chars) {
return substr((string) $num, 0, -strlen($chars)) . $chars;
}
Usage:
$number = 5069695;
echo replaceCharsInNumber($number, 'xxx'); //5069xxx
See it in action here: http://codepad.org/XGyVQ1hk
Strings can be treated as arrays, with the characters being the keys:
$id = 1922312; // PHP converts 01922312 => 1 because of that leading zero. Either make it a string or remove the zero.
$id_str = strval($id);
for ($i = 0; $i < count($id_str); $i++)
{
print($id_str[$i]);
}
This should output your original number. Now to do stuff with it, treat it as a normal array:
$id_str[count($id_str) - 1] = 'x';
$id_str[count($id_str) - 2] = 'y';
$id_str[count($id_str) - 3] = 'z';
Hope this helps!
Just convert to string and replace...
$stringId = $id . '';
$stringId = substr($id, 0, -2) . 'XX';
We can replace specific characters in a string using preg_replace(). In my case, I want to replace 30 with 50 (keep the first two digits xx30), in the $start_time which is '1030'.
Solution:
$start_time = '1030';
$pattern = '/(?<=\d\d)30/';
$start_time = preg_replace($pattern, '50', $start_time);
//result: 1050