explode

Unexpected bracket '[' - PHP [duplicate]

若如初见. 提交于 2019-11-26 19:09:34
This question already has an answer here: PHP Array Syntax Parse Error Left Square Bracket “[” [closed] 2 answers I'm writing a small repository for my little app team's Java code, and I have this error all over my code. $base = explode(".", $class)[0]; The problem occurs only with this one line of code, every time. As far as I know, the above is correct PHP syntax, so what's going on? Parse error : syntax error, unexpected '[' in .../mitc/code/index.php on line 27 If you'd like to see the error, it's at http://chancehenrik.x10.mx/mitc/code/ and elsewhere on my site. That's called array

PHP: How can I explode a string by commas, but not wheres the commas are within quotes?

偶尔善良 提交于 2019-11-26 18:58:33
I need to explode my string input into an array at the commas. However the string contains commas inside quotes. Input: $line = 'TRUE','59','A large number is 10,000'; $linearray = explode(",",$line); $linemysql = implode("','",$linearray); Returns $linemysql as: 'TRUE','59','A large number is 10','000' How can I go about accomplishing this, with the explode ignoring the commas inside the quote marks? Since you are using comma seperated values, you can use str_getcsv . str_getcsv($line, ",", "'"); Will return: Array ( [0] => TRUE [1] => 59 [2] => A large number is 10,000 ) It seems you do not

Add a prefix to each item of a PHP array

大城市里の小女人 提交于 2019-11-26 17:23:29
I have a PHP array of numbers, which I would like to prefix with a minus (-). I think through the use of explode and implode it would be possible but my knowledge of php is not possible to actually do it. Any help would be appreciated. Essentially I would like to go from this: $array = [1, 2, 3, 4, 5]; to this: $array = [-1, -2, -3, -4, -5]; Any ideas? Rohit Chopra Simple: foreach ($array as &$value) { $value *= (-1); } unset($value); Unless the array is a string: foreach ($array as &$value) { $value = '-' . $value; } unset($value); An elegant way to prefix array values (PHP 5.3+): $prefixed

undefined offset when using php explode()

微笑、不失礼 提交于 2019-11-26 17:13:55
问题 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

Multi word search in PHP/MySQL

醉酒当歌 提交于 2019-11-26 16:58:56
问题 I'm struggling to create a search that searches for multiple words. My first attempt yielded no results whatsoever and is as follows: require_once('database_conn.php'); if($_POST){ $explodedSearch = explode (" ", $_POST['quickSearch']); foreach($explodedSearch as $search){ $query = "SELECT * FROM jobseeker WHERE forename like '%$search%' or surname like '%$search%' ORDER BY userID LIMIT 5"; $result = mysql_query($query); } while($userData=mysql_fetch_array($result)){ $forename=$userData[

Explode string by one or more spaces or tabs

浪尽此生 提交于 2019-11-26 16:04:25
How can I explode a string by one or more spaces or tabs? Example: A B C D I want to make this an array. $parts = preg_split('/\s+/', $str); To separate by tabs: $comp = preg_split("/[\t]/", $var); To separate by spaces/tabs/newlines: $comp = preg_split('/\s+/', $var); To seperate by spaces alone: $comp = preg_split('/ +/', $var); This works: $string = 'A B C D'; $arr = preg_split('/[\s]+/', $string); The author asked for explode, to you can use explode like this $resultArray = explode("\t", $inputString); Note: you must used double quote, not single. I think you want preg_split : $input = "A

how to convert array values from string to int?

孤街醉人 提交于 2019-11-26 15:02:26
$string = "1,2,3" $ids = explode(',', $string); var_dump($ids); returns array(3) { [0]=> string(1) "1" [1]=> string(1) "2" [2]=> string(1) "3" } I need for the values to be of type int instead of type string. Is there a better way of doing this than looping through the array with a foreach and converting each string to int? Mark Baker You can achieve this by following code, $integerIDs = array_map('intval', explode(',', $string)); sgrodzicki This is almost 3 times faster than explode() , array_map() and intval() : $integerIDs = json_decode('[' . $string . ']', true); So I was curious about the

Explode a paragraph into sentences in PHP

不羁岁月 提交于 2019-11-26 14:36:52
问题 I have been using explode(".",$mystring) to split a paragraph into sentences. However this doen't cover sentences that have been concluded with different punctuation such as ! ? : ; Is there a way of using an array as a delimiter instead of a single character? Alternativly is there another neat way of splitting using various punctuation? I tried explode(("." || "?" || "!"),$mystring) hopefully but it didn't work... 回答1: You can do: preg_split('/\.|\?|!/',$mystring); or (simpler): preg_split('

Hive Explode / Lateral View multiple arrays

情到浓时终转凉″ 提交于 2019-11-26 10:57:35
问题 I have a hive table with the following schema: COOKIE | PRODUCT_ID | CAT_ID | QTY 1234123 [1,2,3] [r,t,null] [2,1,null] How can I normalize the arrays so I get the following result COOKIE | PRODUCT_ID | CAT_ID | QTY 1234123 [1] [r] [2] 1234123 [2] [t] [1] 1234123 [3] null null I have tried the following: select concat_ws(\'|\',visid_high,visid_low) as cookie ,pid ,catid ,qty from table lateral view explode(productid) ptable as pid lateral view explode(catalogId) ptable2 as catid lateral view

PHP explode the string, but treat words in quotes as a single word

倾然丶 夕夏残阳落幕 提交于 2019-11-26 10:29:58
How can I explode the following string: Lorem ipsum "dolor sit amet" consectetur "adipiscing elit" dolor into array("Lorem", "ipsum", "dolor sit amet", "consectetur", "adipiscing elit", "dolor") So that the text in quotation is treated as a single word. Here's what I have for now: $mytext = "Lorem ipsum %22dolor sit amet%22 consectetur %22adipiscing elit%22 dolor" $noquotes = str_replace("%22", "", $mytext"); $newarray = explode(" ", $noquotes); but my code divides each word into an array. How do I make words inside quotation marks treated as one word? You could use a preg_match_all(...) :