How to split string into alphabetic and numeric components? [duplicate] - php

This question already has answers here:
Split a comma-delimited string into an array?
(8 answers)
Closed 6 years ago.
How do I split a string by . delimiter in PHP? For example, if I have the string "a.b", how do I get "a"?

explode does the job:
$parts = explode('.', $string);
You can also directly fetch parts of the result into variables:
list($part1, $part2) = explode('.', $string);

explode('.', $string)
If you know your string has a fixed number of components you could use something like
list($a, $b) = explode('.', 'object.attribute');
echo $a;
echo $b;
Prints:
object
attribute

$string_val = 'a.b';
$parts = explode('.', $string_val);
print_r($parts);
Documentation: explode

The following will return you the "a" letter:
$a = array_shift(explode('.', 'a.b'));

Use:
explode('.', 'a.b');
explode

$array = explode('.',$string);
Returns an array of split elements.

To explode with '.', use:
explode('\\.', 'a.b');

Related

how to create simple array(without key) by putting available values 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 values like
"34,37"
and I want to create array using this values, array like
array('34','37')
How to create such array if I have values.
hope this will help you :
you can use explode function if you have values as string;
$string = '34,37';
$data = explode(',',$string):
print_r($data); /*output array*/
for more : http://php.net/manual/en/function.explode.php
If you have a string like this:
$str = "1,2,3,4,5,6";
And you want to convert it into array, just use explode()
$myArray = explode(',', $str);
var_dump($myArray);
Have a look here for more information
As per my comment, you should use preg_split function. for more details about preg_split function please read PHP manual and also you can use explode function Explode function PHP manual
<?php
$string = '34,37';
$keywords = preg_split("/[\s,]+/", $string);
//OR $keywords = preg_split("/,/", $string); separated by comma only
print_r($keywords);
you can check your desired Output here
Try this,
$val = "34,37"
$val = explode(',', $val);
print_r($val);
output of above array:
Array
(
[0] => 34,
[1] => 37
)

How to split a string into two variables using PHP? [duplicate]

This question already has answers here:
how to split a comma seperated string into a two variables using php [closed]
(10 answers)
Closed 8 years ago.
Using PHP how can I split the
Szombathely, Hungary
into two variables
Szombathely
Hungary
Thank you!
array explode ( string $delimiter , string $string [, int $limit ] )
Source: http://php.net/manual/en/function.explode.php
$split = explode(',','Szombathely, Hungary');
That will, however, leave you with space before _Hungary
So if all are ,_ you could use
$split = explode(', ','Szombathely, Hungary');
or use trim on each of array elements.
There is a native command called explode, in which you pass as parameters the delimiter and also the string. More info here
In your case it would be:
$result = explode ( ',', 'Szombathely, Hungary')
as a result you get:
$result[0] = 'Szombathely'
$result[1] = 'Hungary'
Hope it helps!
$string = "Szombathely, Hungary";
$string = explode(", ", $string);
list($Szombathely, $Hungary) = $string;
var_dump($Szombathely); //Szombathely
var_dump($Hungary);//Hungary`

how can I split a string with different delimiters into an array? [duplicate]

This question already has answers here:
PHP - split String in Key/Value pairs
(5 answers)
Closed 8 years ago.
How can I split a string with different delimiters into an array?
i.e. convert this: 'web:427;French:435' to this:
'web' => 427,
'french' => 435
This will work as long as your string does not contain & or =.
$str = 'web:427;French:435';
$str = str_replace([';', ':'], ['&', '='], $str);
parse_str($str, $array);
print_r($array);
As mario pointed out, if you don't mind using regex you can amend this answer to fit your needs. If you wish to do it without regex try this: (will work as long as your string doesn't have : and ; inside the variable names or values)
$str = 'web:427;French:435';
$array = explode(';',$str); // first explode by semicolon to saparate the variables
$result = array();
foreach($array as $key=>$value){
$temp = explode(':',$value); // explode each variable by colon to get name and value
$array[$temp[0]]= $temp[1];
}
print_r($result);

Remove an item from a comma-separated string [duplicate]

This question already has answers here:
Fastest way of deleting a value in a comma separated list
(4 answers)
Closed 2 years ago.
Let's say I have a string:
cat,mouse,dog,horse
Is there a regex or a function that will work as follows?
1)"cat" return string ->"mouse,dog,horse"
2)"mouse" return string ->"cat,dog,horse"
3)"dog" return string ->"cat,mouse,horse"
4)"horse" return string ->"cat,mouse,dog"
I need to eliminate the selected item from the string and return the remaining parts of the string.
You mean a function that removes a certain element? Try this:
function removeFromString($str, $item) {
$parts = explode(',', $str);
while(($i = array_search($item, $parts)) !== false) {
unset($parts[$i]);
}
return implode(',', $parts);
}
Demo
It's as simple as exploding the string (str_getcsv) and then removing the searched term. If you have an array, then array_diff makes it very simple:
return array_diff(str_getcsv($list), array($search));
Working demo.
This converts both string inputs to arrays, using explode() for the list. Then you just do array_diff() to output what's in the second array but not the first. Finally we implode() everything back into CSV format.
$input = 'cat';
$list = 'cat,mouse,dog,horse';
$array1 = Array($input);
$array2 = explode(',', $list);
$array3 = array_diff($array2, $array1);
$output = implode(',', $array3);

PHP: Split string [duplicate]

This question already has answers here:
Split a comma-delimited string into an array?
(8 answers)
Closed 6 years ago.
How do I split a string by . delimiter in PHP? For example, if I have the string "a.b", how do I get "a"?
explode does the job:
$parts = explode('.', $string);
You can also directly fetch parts of the result into variables:
list($part1, $part2) = explode('.', $string);
explode('.', $string)
If you know your string has a fixed number of components you could use something like
list($a, $b) = explode('.', 'object.attribute');
echo $a;
echo $b;
Prints:
object
attribute
$string_val = 'a.b';
$parts = explode('.', $string_val);
print_r($parts);
Documentation: explode
The following will return you the "a" letter:
$a = array_shift(explode('.', 'a.b'));
Use:
explode('.', 'a.b');
explode
$array = explode('.',$string);
Returns an array of split elements.
To explode with '.', use:
explode('\\.', 'a.b');

Categories