explode

Stop explode after certain index

不想你离开。 提交于 2019-11-28 08:52:31
问题 How can I stop explode function after certain index. For example <?php $test="The novel Prognosis Negative by Art Vandelay expresses protest against many different things. The story covers a great deal of time and takes the reader through many different places and events, as the author uses several different techniques to really make the reader think. By using a certain type of narrative structure, Vandelay is able to grab the reader’s attention and make the piece much more effective and

Split into two variables?

非 Y 不嫁゛ 提交于 2019-11-28 07:01:03
问题 Say I have the following: "44-xkIolspO" I want to return 2 variables: $one = "44"; $two = "xkIolspO"; What would be the best way to do this? 回答1: Try this: list($one, $two) = split("-", "44-xkIolspO", 2); list($one, $two) = explode("-", "44-xkIolspO", 2); 回答2: PHP has a function called preg_split() splits a string using a regular expression. This should do what you want. Or explode() might be easier. $str = "44-xkIolspO"; $parts = explode("-", $str); $one = $parts[0]; $two = $parts[1]; 来源:

php explode: split string into words by using space a delimiter

感情迁移 提交于 2019-11-28 05:56:40
$str = "This is a string"; $words = explode(" ", $str); Works fine, but spaces still go into array: $words === array ('This', 'is', 'a', '', '', '', 'string');//true I would prefer to have words only with no spaces and keep the information about the number of spaces separate. $words === array ('This', 'is', 'a', 'string');//true $spaces === array(1,1,4);//true Just added: (1, 1, 4) means one space after the first word, one space after the second word and 4 spaces after the third word. Is there any way to do it fast? Thank you. For splitting the String into an array, you should use preg_split :

PHP: Undefined offset

别说谁变了你拦得住时间么 提交于 2019-11-28 05:38:45
问题 On some pages, i receive the error: PHP Notice: Undefined offset: 1 in /var/www/example.com/includes/head.php on line 23 Here is the code: if ($r) { list($r1, $r2)=explode(" ", $r[0],2); $r1 = mb_strtolower($r1); $r3 = " "; $r2 = $r3.$r2; $r[0] = $r1.$r2; $page_title_f = $r[0]." some text"; $page_title_s = "some text "; $page_title = $page_title_s.$page_title_f; } Line 23 with error: list($r1, $r2)=explode(" ", $r[0],2); Could you help to understand what could be the problem? Update Thanks

explode textarea php (at new lines)

走远了吗. 提交于 2019-11-28 04:58:51
can I do: explode("\n", $_POST['thetextarea']); and have it work on all platforms? (The question I am asking is will it ever be \r\n and not just \n") EDIT: I forgot to mention that I am saving $_POST['thetextarea'] to a mysql database VARCHAR 255. It seems \r\n is converted to \n. This will do the trick given \r\n , \r or \n : preg_split('/\r\n|[\r\n]/', $_POST['thetextarea']) You should use: explode("\r\n", $_POST['thetextarea']); It will always be the same. Browsers and other user-agents will make sure they are :-) See http://www.w3.org/MarkUp/html-spec/html-spec_8.html#SEC8.2.1 for more

Shortcut for: $foo = explode(“ ”, “bla ble bli”); echo $foo[0]

坚强是说给别人听的谎言 提交于 2019-11-28 04:47:24
问题 is there a way to get the n-th element of a splitted string without using a variable? My PHP code always looks like this: $foo = explode(" ", "bla ble bli"); echo $foo[0]; Is there a shorter way maybe like in Python? print "bla ble bli".split(" ")[0] Thanks in advance. 回答1: This is what people should be using instead of explode most of the time: $foo = strtok("bla ble bli", " "); It cuts off the first string part until the first " " . If you can't let go of explode, then the closest idiom to

Explode multiple columns in Spark SQL table

流过昼夜 提交于 2019-11-28 03:51:57
问题 There was a question regarding this issue here: Explode (transpose?) multiple columns in Spark SQL table Suppose that we have extra columns as below: **userId someString varA varB varC varD** 1 "example1" [0,2,5] [1,2,9] [a,b,c] [red,green,yellow] 2 "example2" [1,20,5] [9,null,6] [d,e,f] [white,black,cyan] To conclude an output like below: userId someString varA varB varC varD 1 "example1" 0 1 a red 1 "example1" 2 2 b green 1 "example1" 5 9 c yellow 2 "example2" 1 9 d white 2 "example2" 20

undefined offset when using php explode()

断了今生、忘了曾经 提交于 2019-11-28 00:42:32
I've written what I thought was a very simple use of the php explode() function to split a name into forename and surname: // split name into first and last $split = explode(' ', $fullname, 2); $first = $split[0]; $last = $split[1]; However, this is throwing up a php error with the message "Undefined offset: 1" . The function still seems to work, but I'd like to clear up whatever is causing the error. I've checked the php manual but their examples use the same syntax as above. I think I understand what an undefined offset is, but I can't see why my code is generating the error! this is because

How to explode a multi-line string?

♀尐吖头ヾ 提交于 2019-11-28 00:09:44
问题 I have a string that has different values on each line: $matches="value1 value2 value3 value4 value5 "; I want to explode the whole string in to an array consisting of the values separeted. I know how to explode a space separated string, like explode(' ', $matches) . But how do i use the explode function on this type of string? I tried this: $matches=explode('\n',$matches); print_r($matches); But the result is like: Array ( [0] => hello hello hello hello hello hello hello ) 回答1: You need to

Exploding by Array of Delimiters

家住魔仙堡 提交于 2019-11-27 19:46:58
Is there any way to explode() using an array of delimiters? PHP Manual: array explode ( string $delimiter , string $string [, int $limit ] ) Instead of using string $delimiter is there any way to use array $delimiter without affecting performance too much? Use preg_split() with an appropriate regex. $str = 'Monsters are SUPER scary, bro!'; $del = array('a', 'b', 'c'); // In one fell swoop... $arr = explode( $del[0], str_replace($del, $del[0], $str) ); function explode_by_array($delim, $input) { $unidelim = $delim[0]; $step_01 = str_replace($delim, $unidelim, $input); //Extra step to create a