How to remove a specific number from string in PHP [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
How can I remove a specific number from string, if the number is repeated I just need to remove it one time.
$string = "1 3 5 15 5";
//Remove 5
$valuetoremove = "5";
$newvalue = "1 3 15 5";
I tried
str_replace but this remove all the numbers repeated.
str_replace("5"," ","1 3 5 15 5");
$newvalue = "1 3";

If you specifically want to just remove it one time, then I guess this is what you're after
$string = "1 3 5 15 5";
$valueToRemove = "5";
$exploded = explode(" ", $string);
// $exploded = ["1", "3", "5", "15", "5"];
$key = array_search($valueToRemove, $exploded, true);
if ($key !== false) {
unset($exploded[$key]);
}
$newValue = implode(" ", $exploded);

Related

How to split one group into seperate using php [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want split one group AS follow
splitGroups("11133355557777") ➞ ["111", "333", "5555", "7777"]
Anyone have idea then let me know
With php version 5 above this will work link to execute
Link to an example https://paiza.io/projects/qfRZ07OP3OviWCVsUbdFtQ
function splitGroups($str){
$arr = [];
$i=0;
$sub = '';
while($i!=strlen($str))
{
$sub .= $str[$i];
if ( strlen($str)-1 == $i || $str[$i] != $str[$i+1] ){
$arr[] = $sub;
$sub='';
}
$i++;
}
return $arr;
}

How to get the position of same elements in php? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I need your help to get the position of duplicate or same elements in an array.
For example
$arr =[6,5,3,7,40,45,7,6,3,32,86,40,5,3,7,40];
Result
6 = 0,7
5 = 1,11
3 = 2,8
7 = 3,6,14
40 = 4,11,15
45 =5 and so on.
One simple approach:
<?php
$arr =[6,5,3,7,40,45,7,6,3,32,86,40,5,3,7,40];
$pos = array();
foreach($arr as $k => $v) {
$pos[$v][] = $k;
}
foreach($pos as $k => $v) {
echo $k."=".implode(',', $v)."<br>";
}
?>
Result:
6=0,7
5=1,12
3=2,8,13
7=3,6,14
40=4,11,15
45=5
32=9
86=10

How do i get remaining split string in one line of string or useing substr() function [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have string ces123456789101112131415
in the code below. I want to be able to start split from 123456789101112131415 and alter result in index size then try to get the remaining unsplit text in just one line string example expecting 1234 5678 9101 112131415 In result
I have tried:
$code = "ces123456789101112131415";
$start = preg_split("/ces/", $code);
$result = str_split($start[1], 4);
for ($b = 0; $b<3; $b++);
{
echo $result[$b];
}
$total= 0;
$cnt = strlen($result[$b]);
$total += $cnt;
$pay = substr($code,$total);
echo "remain $pay";
but I get 1234 5678 9101 123456789101112131415 as result
Am expecting just 1234 5678 9101 112131415 thanks in advance
This finds ces in string and skips 12 characters, then captures the following digits.
Is that what you are trying to do?
$code = "ces123456789101112131415";
Preg_match("/ces.{12}(\d+)/", $code, $match);
$pay = $match[1];
Echo $pay;
https://3v4l.org/FQ5iX

how to add zeros to string in php [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have strings: 23-65, 123-45, 2-5435, 345-4
I want to add zeros to them so all of them will look like ###-#### (three digits dash four digits): 023-0065, 123-0045, 002-5435, 345-0004
How can i do it in php?
Thanks!
You will need to split them using
$parts = explode('-', $number);`
then use str_pad function:
$parts[0] = str_pad($parts[0], 3, "0");
$parts[1] = str_pad($parts[0], 4, "0");
and then concatenate them again
$number = implode('-', $parts);
Alternatively you can pad them using vsprintf:
$number = vsprintf('%03d-%04d', $parts);
Try:
$str = "23-65, 123-45, 2-5435, 345-4";
$numArray = explode(",",$str);
$str_new = "";
foreach($numArray as $nums) {
$nums = explode("-",$nums);
$num1 = str_pad($nums[0], 3, '0', STR_PAD_LEFT);
$num2 = str_pad($nums[1], 4, '0', STR_PAD_LEFT);
$str_new .= $num1."-".$num2.",";
}
$str_new = rtrim($str_new,",");
Output:
023-0065, 123-0045,0 2-5435, 345-0004

How To Display Random Results in PHP [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Im trying to display 2 or more unique random results from a text file, How do I do that please?
But I need unique results and not 2 or more of the same.
Currently using this code, but it only returns 1 random result:
<?php
$text = file_get_contents('flatFileDB.txt');
$textArray = explode("\n", $text);
$randArrayIndexNum = array_rand($textArray);
$randPhrase = $textArray[$randArrayIndexNum];
?>
<?php echo $randPhrase ?>
I would use something like that
shuffle($textArray);
echo $textArray[0];
echo $textArray[1];
http://php.net/manual/tr/function.shuffle.php
You can give a shot to this also. Code is not tested. I am collecting the used into an array, and check, is that used before.
$text = file_get_contents('flatFileDB.txt');
$textArray = explode("\n", $text);
$used = array();
$countOfRandoms = 2;
$randoms = array();
$i = 1;
do {
if ($countOfRandoms == $i) {
break;
}
$randArrayIndexNum = array_rand($textArray);
if (in_array($randArrayIndexNum, $used)) {
continue;
}
$used[] = $randArrayIndexNum;
$random = $textArray[$randArrayIndexNum];
$i++;
} while (true);

Categories