explode

How to explode the string back from the second or third string delimiter?

百般思念 提交于 2019-12-11 16:42:44
问题 How can I get the 800-555 from this 800-555-5555 with explode() ? Here is a good example: $rawPhoneNumber = "800-555-5555"; $phoneChunks = explode("-", $rawPhoneNumber); First chunk = $phoneChunks[0]; //800 Second chunk = $phoneChunks[1]; //555 Third Chunk chunk = $phoneChunks[2]; //5555 But how can I get the 800-555? Okay, I see, here need more comment... So, this is only an example... In real I add a word (now $word) to string delimiter and my string is a full article... I want that, if

PHP - Exploding / Moving / Filename

橙三吉。 提交于 2019-12-11 16:08:38
问题 Wonder if you can help, i'm new to php and am stuck. I have loads of files that look like this: 2014-04-01 NS122345 - The date, the initials of the person and there employee code. I want to be able to move the files that have NS or JB Or GA into there relevant folder/directories. So for NS it would go into the Nathan Saunders Folder, for JB into the Joe Bailey folder. A few have said to me to use the explode and substr to split the filename up and then i guess search through the array.

PHP Split string in key value pairs

浪尽此生 提交于 2019-12-11 10:37:20
问题 I know this has been asked before but this is a bit different. I have a string like: [de]Text1[fr]Text2[en]Text3 that I need to split in key-value pairs like array('de'=>'Text','fr'=>'Text','en'=>'Text') I do it like this at the moment, but this is not very elegant (and produces an empty object at the first place in the array: $title = '[de]Text1[fr]Text2[en]Text3'; $titleParts = explode('[',$title); $langParts; foreach($titleParts as $titlePart){ $langPart = explode(']',$titlePart);

Ignore last data explode is taking? PHP

心不动则不痛 提交于 2019-12-11 10:30:10
问题 let's say my code is. <?php $var = "1,2,3,4,5,6,"; $var_explode = explode(',', $var); foreach ($var_explode as $number) { echo "$number test"; } ?> And when it echos, it goes like, 1 test, 2 test, 3 test, 4 test, 5 test, 6 test, test. The last one is un-needed, I know its caused because I have a comma after the 6 in my variable, but I need that comma there, not going to remove it. Thanks! 回答1: You can use trim($var, ",") to remove the last comma when passing the string to the explode. $var

What is a better way to replace IDs in an array with their value counterpart?

走远了吗. 提交于 2019-12-11 08:48:06
问题 I have the following array that includes id: [Key1] => 1 [Key2] => 2, 3 I would like to replace these ids by their respective name from this second array: [0] => Array ( [ID] => 1 [Name] => Name1 ) [1] => Array ( [ID] => 2 [Name] => Name2 ) [2] => Array ( [ID] => 3 [Name] => Name3 The desired output: [Key1] => Name1 [Key2] => Name2, Name3 I have the following code which works but I know this is not the right way. If anybody could let me know what would be a better way to achieve this, it

How to access array index when using explode() in the same line?

允我心安 提交于 2019-12-11 03:15:48
问题 Can't wrap my head around this... Say, we explode the whole thing like so: $extract = explode('tra-la-la', $big_sourse); Then we want to get a value at index 1: $finish = $extract[1]; My question is how to get it in one go, to speak so. Something similar to this: $finish = explode('tra-la-la', $big_sourse)[1]; // does not work Something like the following would work like a charm: $finish = end(explode('tra-la-la', $big_sourse)); // or $finish = array_shift(explode('tra-la-la', $big_sourse));

PHP problems with current url

百般思念 提交于 2019-12-11 03:02:55
问题 I use the "Current url" function to get the current link when user changing page language $uri = explode('&', $_SERVER['REQUEST_URI']); $uri = $uri[0]; $url = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$uri : "http://".$_SERVER['SERVER_NAME'].$uri; Problem is, when I have a link like this: http://127.0.0.1/index.php?id=shop&id2=13&lang=lt id2 , of course, disappears. What can I do about this? It is possible if id2 is set to use explode with a second & or something like

Explode a row to multiple rows in pandas dataframe

不打扰是莪最后的温柔 提交于 2019-12-11 01:54:14
问题 I have a dataframe with the following header: id, type1, ..., type10, location1, ..., location10 and I want to convert it as follows: id, type, location I managed to do this using embedded for loops but it's very slow: new_format_columns = ['ID', 'type', 'location'] new_format_dataframe = pd.DataFrame(columns=new_format_columns) print(data.head()) new_index = 0 for index, row in data.iterrows(): ID = row["ID"] for i in range(1,11): if row["type"+str(i)] == np.nan: continue else: new_row = pd

How to match PHP's explode(';',$s,3) to s.split(';',3) in JavaScript?

蓝咒 提交于 2019-12-11 01:29:20
问题 If you run an explode in PHP with the resulting array length limited, it will append the remainder of the string to the last element. This is how exploding a string should behave, since nowhere in the split am I saying that I want to discard my data, just split it. This is how it works in PHP: # Name;Date;Quote $s = 'Mark Twain;1879-11-14;"We haven\'t all had the good fortune to be ladies; we haven\'t all been generals, or poets, or statesmen; but when the toast works down to the babies, we

Is there a function equivalent to Hive's 'explode' function in Apache Impala?

萝らか妹 提交于 2019-12-11 01:01:40
问题 Hive's function explode is documented here It is essentially a very practical function that generates many rows from a single one. Its basic version takes a column whose value is an array of values and produces a copy of the same row for each of those values. I wonder whether such a thing exists in Impala. I haven't been able to find it in the documentation. 回答1: Impala does not have any function like EXPLODE in hive to read complex data types and generate multiple rows. Currently through