This question already has answers here:
How to represent a PHP array as a comma-delimited string? [duplicate]
(2 answers)
Closed 8 years ago.
i want to make all array values in one variable
Example:
i have a title
$title = "my name is medo";
$words = explode(' ', $title);
The result is a
$words['0'] = my
$words['1'] = name
$words['2'] = is
$words['3'] = medo
i want to make it like that
$allwords = "my,name,is,medo";
thanks all
You can use implode:
implode ( "," , $words );
You can also do this with string functions, which is much easier for your case.
$originalString = "my name is medo";
$finalString = str_replace(" ", ",", $originalString);
Related
This question already has answers here:
PHP - split String in Key/Value pairs
(5 answers)
Parse query string into an array
(12 answers)
Closed 1 year ago.
I have the following string:
$string = "No+Label+name=Aaron+Smith&No+Label+email=aaron.smith#live.co.uk";
I'd like some PHP to grab the name and email address values from this string.
I'd originally tried the following but made no progress:
$str = "No+Label+name=Aaron+Smith&No+Label+email=aaron.smith#live.co.uk";
preg_match_all('#value=([^\s]+)#', $str, $matches);
echo implode(' ', $matches[1]);
I'd essentially like to say:
echo $string['No+Label+name']; // outputs name
Not a great e answer but you can use this if you have this string
$str = "No+Label+name=Aaron+Smith&No+Label+email=aaron.smith#live.co.uk";
$temp = explode('+', $str);
$name = explode('=',$temp[2]);
$email = explode('=',$temp[5]);
echo ($name[1]);
echo "<br/>";
echo ($email[1]);
This question already has an answer here:
How to replace multiple items from a text string in PHP? [duplicate]
(1 answer)
Closed 6 years ago.
I have the following string:
$string = java-developer-jobs
I would like to remove - and jobs from $string, but I don't want to use str_replace multiple times.
Use array as :
$toReplace = array('-','jobs');
$with = array('','');
$string = 'java-developer-jobs';
str_replace($toReplace,$with,$string);
You can use regex for this.Try something like this:
$string = "java-developer-jobs";
$sentence = preg_replace('/\-|jobs/', ' ', $string);
echo $sentence;
Output: java developer
LIVE DEMO
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>";
}
?>
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:
How to trim white spaces of array values in php
(15 answers)
Closed 9 years ago.
If I have this array:
array(" hey ", "bla ", " test");
and I want to trim all of them, How can I do that?
The array after the trim:
array("hey", "bla", "test");
array_map() is what you need:
$result = array_map('trim', $source_array);
array_map() applies a given callback to every value of an array and return the results as a new array.
$array = array_map('trim', $array);