PHP - Errors Using Preg_split To Separate By Commas
I am trying to split some strings up by commas with preg_split but I keep on getting the error: "No ending delimiter ',' found"
Here is my Code: $links = preg_split(',', $links); $titles = preg_split(',', $titles); $images = preg_split(',', $images); Thanks, Alex Similar TutorialsCan get this to work.. please help. $string="[url=http://www.randomsite.com]Click me[/url]"; $splitted=preg_split("/\[url=http://.*\]/",$string); print_r($splitted); preg is WITHOUT http:// forum added it just url= I need to extract this: "Click me[/url]" hope to input the below data via $_POST into a while loop to preg_split by comma and then recursively create xml element tag with the various firstName lastName's and even separate further aliasFirstName aliasLastName's into an attribute field NAMES: firstName lastName [aliasFirstName aliasLastName], firstName lastName [aliasFirstName aliasLastName], firstName lastName [aliasFirstName aliasLastName], firstName lastName [aliasFirstName aliasLastName] $xml .= ' <element1 attribute="' . $_POST('AGE') . '">'; while ($row = ' . $_POST('NAMES') . ') { $row = preg_split('/[\s]*[,][\s]*/',' . $_POST["Actors"] . ',PREG_SPLIT_OFFSET_CAPTURE); $xml .= ' <element2> <NAME names="' . $_POST('NAMES') . '" alias="' . $_POST('NAMES') . '"></NAME> $xml .= ''; <?php echo "How much money do you want landed on the account?!\n"; $landed = fgets(STDIN); $calc = ($landed * .25); $total = ($landed + $calc); echo "\nYou need to list the dummy for $".$total."...\n\n"; echo "Press enter to close this script..."; $exit = fgets(STDIN); ?> so say I enter 12000000000 which would make the $total .. $15000000000 how so I make the total have commas in it, like $15,000,000,000 I'm trying to parse a csv file. The user sometimes enters commas into a field in Excel. Excel will then put quotes around that field that has commas in it when creating the csv file. Now my fields are all off because of these user entries with commas. I tried using an fgetcsv thinking it may handle this situation but I get an error with an "Array to string conversion" . How do I manipulate these lines that have commas within quotes in a csv file? $student = trim(fgets($fh)); $line = explode(",",$student); I was wondering how you would go about separating words with commas between them into an array. In this case I want users to be able to message multiple other users by entering their names in the following format: user1, user2, user3, user4 and then have php separate those into an array so I can then run them through a foreach(); so they can be sent to the correct users... Any help or functions I should look into would be great... code not necessary, just point me in the right direction thanks! Ok so I have this script that I wrote and it works really good. I wanted to display the number of records in a table and since the db only changes once per day I thought I would put the couner on the back end. I have this script that changes the value of the text file every day. But I would love it if it outputted with commas.
Now it reads:
1234070
Want it to read:
1,234,070
Any thoughs?
#!/usr/bin/php -q <?php $username=""; // READONLY USER USED HERE AND AUTH'D ON LOCALHOST ONLY $password=""; $database=""; $dbhost=""; mysql_connect($dbhost,$username,$password); @mysql_select_db($database) or die( "Unable to connect to database please try again!"); $counthd=mysql_query("SELECT count(*) as total from HD"); $hdtotal=mysql_fetch_assoc($counthd); $dbcount = $hdtotal['total']; echo ""; $myFile = "dbcounter.txt"; $fh = fopen($myFile, 'w'); fwrite($fh, "$dbcount"); fclose($fh); ?> Edited by chadrt, 01 February 2015 - 07:26 PM. Hi! So what I'm trying to do is split a multi dimensional array of strings ( each containing 3 strings inside of 1 array ) so I can write the results to a file. So say I had a bunch of strings inside 1 array : class File { function WriteFile ( $filename, $content ) { $list = [ $content ]; $delimiters = File :: checkDelimiter ( $content, true, false ); $fp = fopen ( $filename, 'w' ); foreach ( $list as $fields ) { $fields = File :: SplitFileByDelimiter ( $fields, ",", 3 ); file_put_contents ( $filename, $fields ); } fclose ( $fp ); } } $file-> File :: WriteFile ( $filepath . $filename, "TEST!, TEST!, TEST!, " . "TEST!, TEST!, TEST!, " . "TEST!, TEST!, TEST!" ); Instead of writing : TEST!, TEST!, TEST!, TEST!, TEST!, TEST!, TEST! ,TEST!, TEST!, TEST! to the file in a straight line, it should write it like this : TEST!, TEST!, TEST!, TEST!, TEST!, TEST!, TEST!, TEST!, TEST! Any help is ABSOLUTELY appreciated! Edited May 22 by AquariaXI Hi, I am trying to find a way that will allow me to convert information passed via POST into array so I can run foreach on the array. For example if information in $_POST[ids] is 1000, 1001, 1002, 1003 I would like to get it into an array $array = array("1000", "1001", "1002", "1003") Thanks I have some data in a table and some of it is Artist names stored as "Last, First" I need to be able to have the script search weather or not someone types "last, first" or "first last". Any ideas? Here's my code: <html> <head> <title>search script</title> </head> <body> <form name="form" action="search.php" method="get"> <input type="text" name="q" /> <input type="submit" name="Submit" value="Search" /> </form> <?php // Get the search variable from URL $var = @$_GET['q'] ; $trimmed = trim($var); //trim whitespace from the stored variable // rows to return $limit=100; // check for an empty string and display a message. if ($trimmed == "") { echo "<p>Please enter a search...</p>"; exit; } // check for a search parameter if (!isset($var)) { echo "<p>We dont seem to have a search parameter!</p>"; exit; } //connect to your database ** EDIT REQUIRED HERE ** mysql_connect("localhost","username","password"); //(host, username, password) //specify database mysql_select_db("mydb") or die("Unable to select database"); //select which database we're using // Build SQL Query $query = "select * from songs where Title like \"%$trimmed%\" or Artist like \"%$trimmed%\" order by Title"; // EDIT HERE and specify your table and field names for the SQL query $numresults=mysql_query($query); $numrows=mysql_num_rows($numresults); // If we have no results, offer a google search as an alternative if ($numrows == 0) { echo "<h4>Results</h4>"; echo "<p>Sorry, your search: "" . $trimmed . "" returned zero results</p>"; } // next determine if s has been passed to script, if not use 0 if (empty($s)) { $s=0; } // get results $query .= " limit $s,$limit"; $result = mysql_query($query) or die("Couldn't execute query"); // display what the person searched for echo "<p>You searched for: "" . $var . ""</p>"; // begin to show results set echo "Results"; $count = 1 + $s ; // now you can display the results returned echo "<table border=1>"; while ($row= mysql_fetch_array($result)) { $title = $row["Title"]; $artist = $row["Artist"]; $number = $row["Number"]; echo "<tr><td>$count.)</td><td>$title</td><td>$artist</td><td>$number</td></tr>" ; $count++ ; } echo "</table>"; $currPage = (($s/$limit) + 1); //break before paging echo "<br />"; // next we need to do the links to other results if ($s>=1) { // bypass PREV link if s is 0 $prevs=($s-$limit); print " <a href=\"$PHP_SELF?s=$prevs&q=$var\"><< Prev 10</a>  "; } // calculate number of pages needing links $pages=intval($numrows/$limit); // $pages now contains int of pages needed unless there is a remainder from division if ($numrows%$limit) { // has remainder so add one page $pages++; } // check to see if last page if (!((($s+$limit)/$limit)==$pages) && $pages!=1) { // not last page so give NEXT link $news=$s+$limit; echo " <a href=\"$PHP_SELF?s=$news&q=$var\">Next 20 >></a>"; } $a = $s + ($limit) ; if ($a > $numrows) { $a = $numrows ; } $b = $s + 1 ; echo "<p>Showing results $b to $a of $numrows</p>"; ?> </body> </html> How do I display the actual errors in IIS 7.5? If I miss a semicolon, I get: "HTTP Error 500.0 - Internal Server Error" absolutely useless. In prior versions, I could see the line and get to the PHP error. How do I display PHP errors? I've added: set_ini('display_errors', '1'); but it doesn't help. I have the following function, which takes a string with commas in it and attempts remove those commas. The way I have it here is that I use explode to take out the commas, which makes an array, and then iterate through that array to put a string back together without the commas. function tags_to_sort_by( $sortMeta ) { $sortByArray = explode(",", $sortMeta); $sortByCount = count($sortByArray); for ($i = 0; $i < $sortByCount; $i++) { $sortByString += $sortByArray[$i]; } return $sortByString; } What does not work is the following line: $sortByString += $sortByArray[$i]; Somehow that outputs a 0, which I don't understand. It should output something like: arrayItem1 arrayItem2 array3 etc. My question is if there either is an easier way to remove the commas from the original string or what I am doing wrong in this line: $sortByString += $sortByArray[$i]; // I am trying to concatenate each part of the array back into the string. Thanks a lot for help with this! For example if $names has a value of "Mark/Ben/James/Tom" how would I separate it to give each name its own variable, e.g $name1, $name2, $name3
Thanks,
Hi all, I'm currently working on a webpage that dynamically shows tables of sales for products. I'm using SQL to store 2 tables, one containing product info (name, price etc.) and one containing sales for each product. One on of my web pages I want to show the product name and sales for a single year. This would involve ripping the product name from my product table and the sales and year from my sales table. Is this possible in one query? $Query= "select name from productTable and sales, year from salesTable where year = $desiredyear; Obviously I know this is wrong because it isn't working but am I even on the right lines? Thank you. What is the most secure way? Having DB connect script in the beginning of every script you need db for, or Having a database.php script containing the script and then including it to all other php. here is my code $query = "SELECT * from links ORDER BY field_4 DESC"; // assumes that your date column is a DATE data type so that ordering by it will sort the dates $result = mysql_query($query); // check if the query executed without error and if it returned any rows here... // retrieve and process the data from the query $last_date = ''; // initialize to a value that will never exist as data while($row = mysql_fetch_assoc($result)){ // test for a new date and output the date heading if($last_date != $row['field_4']){ echo "<p><font face='Arial, Helvetica, sans-serif'>"; echo $row['field_4'] . "<br />"; echo "</font></p>"; $last_date = $row['field_4']; // remember the new date } // output the data (under each date heading) echo "<img src=" . $row['field_1'] . " border=0>"; echo "<a href='" . $row['field_3'] . "'>"; echo $row['field_2'] . "</a><br />"; } mysql_close($con); i would like set the data under each heading to be ordered by a different column, how do i do so? Or is the ORM the only mapper? I've been cracking at this for the part of 2 days now, and I cannot get my regex to work properly in my script. /^[A-Za-z][\w\-',.]*(?<![^A-Za-z][\w\-\'\,\.])$/i The above is for a last name field, and it needs to be able to accept: Roberts, Jr. (as an example)... As you can see in my regex, I am allowing all of these characters.. And it's still not validating it. You can test to see what I mean he <?php $last_name = "Roberts, Jr."; if (!preg_match("/^[A-Za-z][\w\-',.]*(?<![^A-Za-z][\w\-\'\,\.])$/i", $last_name )) { echo "Sorry no dice!"; } ?> I am currently trying to make a line graph out of data using php and javascript. How do I add up the values of a column and separate them by month. For example the database has 74 amount of rows. Then the values from the "views" column get added up and then displayed by month. Code: [Select] <table width="70%" class="linechart"> <thead> <tr> <td></td> <th scope="col">Jan</th> <th scope="col">Feb</th> <th scope="col">Mar</th> <th scope="col">Apr</th> <th scope="col">May</th> <th scope="col">Jun</th> </tr> </thead> <tbody> <tr> <th scope="row">Post Views</th> <td>24324</td> <td>29634</td> <td>15435</td> <td>56545</td> <td>23543</td> <td>2123</td> </tr> </tbody> </table> I've got a table (tbl_items) that holds a number of items and the individual information for each. Within 'tbl_items' I also have a row labeled 'category_id' in which each item is given a number (ex: 2). I've got a separate table (tbl_items_categories) that only has 2 rows: 'category_id' and 'category_name'. I wasn't sure if a JOIN method would be best for this but what would be the best approach to list all items and within each item, also echo out the name of the category they belong to and not the category_id number? ok i think and hope this will be my last issue with this site. i have two forms on the same contact page, but right now theyre using the same error label. this means if ONE has an error, both forms show as that same error. id like to somehow separate this and give each one its own label. i tried doing like error[2] for the 2nd button instead of error[] but then i didnt know what to change the actual "error label" to. do i changed the "$errors = array();" part? to create a second label? i tried to google on it and couldnt figure it out. thanks a million to anyone who even reads <?php if (isset($errors)) { foreach ($errors as $error) { echo("<p>$error<p>\n"); } } ?> so my question basically is, how do i use this code below to create a second error case AND label please!! <?php if ($_POST['send']) { $errors = array(); if ($_POST['captcha'] != $_SESSION['captchacode']) { $errors[] = "You didn't enter the correct letters!"; } + <?php if (isset($errors)) { foreach ($errors as $error) { echo("<p>$error<p>\n"); } } ?> |