How to check explode contain only comma separated values...? [duplicate] - php

This question already has answers here:
Check if String has only Integers separated by comma in PHP
(3 answers)
Closed 6 years ago.
How to check that php explode hold only comma separated integer values.
for example i have a
$str= '10,20,30,40,50,60,70,80,90,100';
$arr = explode(',',$str);
and i want to check if the value in $str is 10,20,30 and so on. and cannot contain any of string and words.
I basically populate a drop-down from this $arr and i want to make check if user enter integer values with comma then its show the dropdown else if user enter any other format.
i also use is_int or is_numeric but it cannot solve my problem.
thanks for advance .

What you're looking for is ctype_digit
$str= '10,20,30,40,50,60,70,80,90,100';
$arr = explode(',',$str);
foreach($arr as $a){
if(!ctype_digit($a)){
//Not an int
}else{
//is an int
}
}
Output:- https://eval.in/734431
Reference:- http://php.net/ctype_digit

Related

How can we convert string into array in php? [duplicate]

This question already has answers here:
Split a comma-delimited string into an array?
(8 answers)
Closed 1 year ago.
I have a string as mention below
$string = 20181123091338,20181130070940;
Now, I want to convert this string into an array format like
$array = array("20181123091338","20181130070940");
So, How can I do this? Please help me.
Thank You
You can use explode() .
Try this
$string = "20181123091338,20181130070940";
$arr = explode(",", $string);
echo "<pre>"; print_r($arr);
Explaination
Here we are exploding string by ","(comma), so we are passing ,(comma) as first parameter in explode function and string passing as second parameter.

Exploding a number and separate it by a space [duplicate]

This question already has answers here:
Insert string at specified position
(11 answers)
separate string in two by given position
(8 answers)
Closed 4 years ago.
I want to explode a string or an integer and separate it by a space.
E.g., I have this int 12345678, and I want its numbers to become like 123 45678. I want the first three numbers separated. Can someone give me a clue or hint in how to achieve this, like what function to use in PHP? I think using an explode will not work here because the explode function needs a separator.
You can use substr_replace() - Replace text within a portion of a string.
echo substr_replace(1234567, " ", 3, 0); // 123 4567
https://3v4l.org/9CFlX
You could use substr() :
$str = "12345678" ;
echo substr($str,0,3)." ".substr($str, 3); // "123 45678"
Also works with an integer :
$int = 12345678 ;
echo substr($int,0,3)." ".substr($int, 3); // "123 45678"
This problem will solve by using substr().
The substr() function returns a part of a string.
Syntax: substr(string,start,length)
Example:
$value = "12345678";
echo substr($value,0,3)." ".substr($value, 3);
Output: 123 45678
You may get better understand from here.

How to cut string after comma [duplicate]

This question already has answers here:
PHP substring extraction. Get the string before the first '/' or the whole string
(14 answers)
Closed 8 months ago.
$A=123132,32132,123132321321,3
$B=1,32,99
$C=456,98,89
$D=1
I want to cut string after first comma
output. . .
$A=123132
$B=1
$C=456
$D=1
$newA = current(explode(",", $A));
from: PHP substring extraction. Get the string before the first '/' or the whole string
You can do this with strpos to get the position of the first comma, and substr to get the value prior to it:
<?php
$val='123132,32132,123132321321,3';
$a=substr($val,0,strpos($val,','));
echo $a;
?>
Output:
123132
There are a number of ways to accomplish this:
substr($A,0,strpos($A,","))
or
current(explode(",", $A))
will both return the value 123132
You can do
$A='123132,32132,123132321321,3';
$t=explode(',',$A);
$result = $t[0];

PHP remove number from comma seperated string stored in db [duplicate]

This question already has answers here:
Fastest way of deleting a value in a comma separated list
(4 answers)
Closed 2 years ago.
I am storing numbers in a database with a string like 5,55,15,17,2,35
I want to remove say number 5 from the string, keeping the comma seperation set like it is.
The problem with using str_replace() is that all of the 5's will be removed. If i use str_replace('5,'', $string) thats fine, but the comma wouldn't be after the 5 if it was in the middle of the string or at the end. That same str_replace would also remove part of 15, and 55,
Am i missing something?
$array = explode(',', $string);
foreach ($array as $k => $v)
if ($v == 5) unset($array[$k]);
$string = implode(',', $array);
You probably shouldn't be storing a comma separated list of values in a single database column in the first place. It looks like a one-to-many association, which should be modeled with a separate table.
Split the string first by comma so you can work with the numbers directly. Remove 5 from the array, then recombine the array into a comma-delimited string.
Here's an example:
<?php
$input = '5,55,15,17,2,35';
echo "Input: $input<br />";
// Split the string by "exploding" the string on the delimiter character
$nums = explode(',', $input);
// Remove items by comparing to an array containing unwanted elements
$nums = array_diff($nums, array(5));
// Combine the array back into a comma-delimited string
$output = implode(',', $nums);
echo "Output: $output<br />";
// Outputs:
// Input: 5,55,15,17,2,35
// Output: 55,15,17,2,35
?>
str_replace([$num.",",",".$num],"",$input);
U can try something like this:
// Convert string into array of numbers
$arrOfNumbers = explode(',', $string);
// Find id of number for del
$numberForDelId = array_search($numberForDel, $arrOfNumbers);
//Delete number
unset($arrOfNumbers[$numberForDelId]);
// Convert back to string
$resultString = implode(',' $arrOfNumbers)
You should:
get the string
then "explode" the values into an array
and re-create the string without the number you want to remove.

Matching words in strings [duplicate]

This question already has answers here:
Php compare strings and return common values
(3 answers)
Closed 8 years ago.
I have two strings of keywords
$keystring1 = "tech,php,radio,love";
$keystring2 = "Mtn,huntung,php,tv,tech";
How do i do return keywords that common in both strings
You can do this:
$common = array_intersect(explode(",", $keystring1), explode(",", $keystring2));
If you want them back into strings, you can just implode it back.
Hmm, interesting question... You can use this.
$arr1 = explode(',',$keystring1);
$arr2 = explode(',',$keystring2);
$duplicates = array_intersect($arr1,$arr2);
foreach($duplicates as $word) {
echo $word;
}
You could explode() both strings on commas into arrays and loop through the first array checking to see if any of the words exist in the second array using the in_array() function. If so then add that word to a "common words" array.
Those are going to need to be arrays not variables.
$keystring1 = array('tech','php','radio','love');
$keystring2 = array('mtn','huntung','php','tv','tech');
First of all...

Categories