This question already has answers here:
Split a comma-delimited string into an array?
(8 answers)
Closed 7 years ago.
i have string like this:
<?php $string = "52.74837280745686,-51.61665272782557"; ?>
i want access to first string before comma and second string after comma like this:
<?php string1 = "52.74837280745686"; $string2 = "-51.61665272782557" ; ?>
thank you !
This is quite straigtforward
<?php
list($string1, $string2) = explode(",", $string);
or more excant
<?php
$splitTokens = explode(",", $string); // this is what "split" is in other languages, splits an spring by a SEP and retrieving an array
$string1 = $splitTokens[0];
$string2 = $splitTokens[1];
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:
PHP explode the string, but treat words in quotes as a single word
(5 answers)
Closed 7 years ago.
I have a string like this |casio|watch|men| in an xml. I want to explode this and i need output like this : $brand = "casio"; $cat = "watch";.
i used these codes but it doesnt helped me
<?php
$string = '|casio|watch|men|';
$string = explode('-',$string);
var_dump($string);
echo $string[0];
echo $string[1];
?>
your trying to separate the string using '-' while your string doesnt contain any... in your case you should explode the using '|' example
$string = explode('|',$string);
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`