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.
Related
This question already has answers here:
PHP replace multiple value using str_replace? [duplicate]
(3 answers)
Closed 1 year ago.
I have a string containing comma-separated numbers and I also have an associative array in PHP containing unique numbers. What I want to achieve is to create a new string that contains only the exact match replacements based on the array. Here is my code so far:
<?php
$replacements = array(
'12' => 'Volvo',
'13' => 'BMW',
'132' => 'Alfa Romea',
'156' => 'Honda',
'1536' => 'Tesla',
'2213' => 'Audi'
);
$str ="12,13,132,2213";
echo str_replace(array_keys($replacements), $replacements, $str);
?>
The only problem is that the output is this:
Volvo,BMW,BMW2,22BMW
instead of this:
Volvo,BMW,Alfa Romeo,Audi
How can I achieve exact match search and replace in this function? Is there a fastest PHP solution for this problem?
Thanks,
belf
Another one-liner. Combine explode the string keys, and flip it to serve as keys using array_flip, then finally, array_intersect_key to combine them by key. implode the remaining values combined:
$cars = implode(', ', array_intersect_key($replacements, array_flip(explode(',', $str))));
You may use this very simple, straight-forward, and easy to understand code:
$result = [];
foreach (explode(',', $str) as $id) {
$result[] = isset($replacements[$id]) ? $replacements[$id] : $id;
}
$str = implode(',', $result);
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
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
)
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`
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);