I have a string $employee="Mr vinay HS,engineer,Bangalore";. i want to store Mr vinay H S in $name ,engineer in $job and Bangalore in $location. how to do this
$record = explode(",",$employee);
$name = $record[0];
$job = $record[1];
$location = $record[2];
Use the list function to take the exploded array into 3 variables
list($name, $job, $location) = explode(',', $employee);
list($name,$job,$location) = explode(',',$employee);
Note: this will obviously only work if the string is in the format you specified. If you have any extra commas, or too few, then you'll have problems.
It's also worth pointing out that PHP has dedicated functions for handling CSV formatted data.
See the manual page for str_getcsv for more info.
While explode() is the quickest and easiest way to do it, using str_getcsv() rather than simple explode() will allow you to handle input more complex input, eg containing quoted strings, etc.
explode will do just that I guess
http://php.net/manual/en/function.explode.php
Any more help on this would suffer from givemethecode-ism, so I leave you to it ;-)
list($name,$job,$location) = explode(',',$employee);
Something like the following should work:
//The employee name
$employee= "Mr vinay HS,engineer,Bangalore";
//Breaks each portion of the employee's name up
list($name, $job, $location) = explode(',',$employee);
//Outputs the employee's credentials
echo "Name: $name; Job: $job; Location: $location";
Or you can use another function to do that - http://php.net/manual/en/function.str-getcsv.php
list($name,$job,$location) = str_getcsv($employee);
You can look at the explode() and list() functions:
list($name, $job, $location) = explode(",", $employee);
Related
Hi in my url i have variables stored in ../index.php?cat=1#5#8 in so on how to separate them using explode function so out put could be
arr[0]=1
arr[1]=5
arr[2]=8
Try with explode like
$arr = explode("#",$_GET['cat']);
But as #Bora said after # the remaining string will may not be sent ,so better to use '_' in place of '#' (1_5_8_....)and can explode it like
$arr = explode("_",$_GET['cat']);
Try this:
$array=$_GET['cat'];
$result = explode("#",$array);
I have an array with SURNAME NAME as value. I would need to invert that, getting NAME SURNAME as result. I've been taking a look at php functions but couldn't find a useful one. Thank you!
$name = array('SMITH JOHN', 'BACON KEVIN', 'MERCURY FREDDIE');
Try this code:
$names = array('SMITH JOHN', 'BACON KEVIN', 'MERCURY FREDDIE');
foreach ($names as &$full_name){ //Remember here to pass by reference &
$parts = explode(" ", $full_name);
$full_name = "{$parts[1]} {$parts[0]}" ;
}
var_dump($names) ;
This should work:
$names = preg_replace('/^(\S+)\s+(\S+)$/', '\2 \1', $names);
edit: Yep, it works just fine.
Please note that this only works for two-part names. If it has more than two parts (e.g. middle name, suffix) it will leave it as-is.
$name = array('SMITH JOHN', 'BACON KEVIN', 'MERCURY FREDDIE');
array_walk(
$name,
function (&$value) {
$value = implode(' ',array_reverse(explode(' ',$value)));
}
);
As Jan pointed out, string parsing is the way to go (take the individual string and parse the parts to compose the new string) but just be warned in general you'll need to make assumptions about the names to do this correctly.
If you're guaranteed to have only LAST FIRST entries, you're fine.
But for example, what about names like
HENRY VAN WINKLE
MADONNA
CATHERINE ZETA-JONES
ROBERT DOWNEY JR.
MR. T
Some cases are clear, but others aren't.
I am trying to parse following string...
IN.Tags.Share({"count":180,"url":"http://domain.org"}
is my following approach correct to get the value of count?
$str = 'IN.Tags.Share({"count":180,"url":"http://domain.org"}';
$data = explode(':', $str);
$val = explode(',', $data[1]);
return $val[0];
Or is there any better way to handling this type of strings? I think it could be done using regex as well.
thanks.
If course I'm not sure if your format will be constant, but part of your string looks like JSON. If always like this, you could do:
$str = str_replace('IN.Tags.Share(', '', $str);
$values = json_decode($str);
echo $values->count;
I would suggest pulling out the JSON by applying this regex to the string: IN\.Tags\.Share\((.*)\. Pull out the first group, and use json_decode: http://php.net/manual/en/function.json-decode.php
That way, you can directly access the data. It will support complex data structures as well.
let's say I have a string called str, I dont know how long is that.
characters in the string are separated by '-' after each 16th character.
Now i called function like $ex = explode('-', $str);.
Now it is in array. I have changed some chracters in array. for example $ex[0][0] = 'a';
Now I want to connect that changed arrays back to variable $str2.
Something like $str2 = $ex[0].ex[1] but I don't know how long is that array.
Do you know how?
IF you didnt understand my explaination, tell me.
Thank you really much.
I think you want implode:
http://php.net/manual/en/function.implode.php
Example:
$str2 = implode('', $ex);
Try:
$str2 = implode('-', $ex);
This will take all of the elements of $ex and connect them into one string with the first parameter between each element. In this case: -.
If you don't want them to be connected by anything, then you can just do:
$str2 = implode($ex);
Use foreach. Foreach allows you to run through the array and automatically stop when the end has been reached.
An example would be:
foreach ($ex as $e) {
$str2 .= $e;
}
If i have a string like this:
$myString = "input/name/something";
How can i get the name to be echoed? Every string looks like that except that name and something could be different.
so the only thing you know is that :
it starts after input
it separated with forward slashes.
>
$strArray = explode('/',$myString);
$name = $strArray[1];
$something = $strArray[2];
Try this:
$parts = explode('/', $myString);
echo $parts[1];
This will split your string at the slashes and return an array of the parts.
Part 1 is the name.
If you only need "name"
list(, $name, ) = explode('/', $myString);
echo "name is '$name'";
If you want all, then
list($input, $name, $something) = explode('/', $myString);
use the function explode('/') to get an array of array('input', 'name', 'something'). I'm not sure if you mean you have to detect which element is the one you want, but if it's just the second of three, then use that.