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

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`

Related

Convert string to array with keys [duplicate]

This question already has answers here:
Parse query string into an array
(12 answers)
Closed 4 years ago.
Wi,
I have string:
sortAfter=1&searchString=vsfweew
How can I convert it to array:
Array
(
[sortAfter] => 1
[searchString] => vsfweew
)
?
$str = 'sortAfter=1&searchString=vsfweew';
parse_str($str, $arr);
print_r($arr);
$str = 'sortAfter=1&searchString=vsfweew';
parse_str($str, $arr);
While parse_str() is certainly convenient, there is another option using preg_split() as follows:
<?php
$str = "sortAfter=1&searchString=vsfweew";
list($e1,$sortAfter,$e2,$searchString) = preg_split("/=|&/",$str);
$arr[$e1] = $sortAfter;
$arr[$e2] = $searchString;
print_r($arr);
See live code
Preg_split() splits a string into an array whose values can then be assigned to a list of variables which include in this case variables that correspond to element names while others refer to element values. The list variables are then used to build a new array $arr.

using str_replace to match whole words /case insensitive [duplicate]

This question already has answers here:
PHP string replace match whole word
(4 answers)
Closed 4 years ago.
I'm trying to replace whole words in a string using str_replace, however, even letters matched in words are being replaced. i tried preg_replace, but couldn't get it to work as i have a big list of words to replace. Any help is appreciated.
$array2 = array('is','of','to','page');
$text = "homepage-of-iso-image";
echo $text1 = str_replace($array2,"",$text);
the output is : home--o-image
For the whole-words issue, there is a solution here using preg_replace(), and your the solution would be to add /\b to the beginning and \b/u to the end of your array-values. The case-insensitive could be handled with preg_replace_callback (see example #1), but if you are working with a small array like your example, I would just recommend duplicating the array-values.
Applied to your example:
$array2 = array(
'/\bis\b/u',
'/\bof\b/u',
'/\bto\b/u',
'/\bpage\b/u',
'/\bIS\b/u',
'/\bOF\b/u',
'/\bTO\b/u',
'/\bPAGE\b/u'
);
$text = "homepage-of-iso-image";
echo $text1 = preg_replace($array2,"",$text);
You could use array_diff
array array_diff ( array $array1 , array $array2 [, array $... ] )
Compares array1 against one or more other arrays and returns the
values in array1 that are not present in any of the other arrays.
<?php
$remove = array('is','of','to','page');
$text = "homepage-of-iso-image";
$parts = explode('-', $text);
$filtered = array_diff($parts, $remove);
print implode('-', $filtered);
Output:
homepage-iso-image

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 assign series of numbers to a single array [duplicate]

This question already has answers here:
Separate space-delimited words in a string [duplicate]
(4 answers)
Closed 7 years ago.
I have a from input that contains series of numbers separate by white space that looks like this:
$input = "1243 984 4692 9465 ";
Is there any way to convert this to an array that keeps those numbers like this looks like this:
$array_numbers[0] = 1243
$array_numbers[1] = 984
$array_numbers[2] = 4692
$array_numbers[3] = 9465
Any help on this will be much appreciated,
Thank you
Try
$array_numbers = explode(' ', trim($input));
Trim and explode will do that for you.
http://php.net/manual/en/function.explode.php
http://php.net/manual/en/function.trim.php
Trim removes the whitespace at the end, which if it stays will cause an empty entry in the array.
$input = "1243 984 4692 9465 ";
$array_numbers = explode(' ', trim($input));
The best way to do this is to use the explode function in php that separate the content of your string into an array of string, then convert each elements of array items into integer using intVal() function in php
See sample code below
<?php
$hello = "12 34 45 56 78";
$result = array();
$result = explode(" ", $hello);
for($j=0; $j<count($result); $j++){
echo intval( $result[$j] )."</br>";
}
?>

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);

Categories