PHP - Get The Longest Number From A String
I have a really weird thing that I have to do, and I can't figure it out. I've tried about 1 million nested loops, and just can't get this to work.
What I need to do is get the longest number from a string. So, for example, take the following: $string = "asdfasd234kjljlkjlj34659823423sadfasdadasdfasdfas d3242sdasddsda2"; function getNumber($a) { //do something with $string and return the longest number from it } so that ... echo getNumber($string); will return '34659823423' Thanks in advance, fellas! I've really been racking my brain there. Similar TutorialsHi I've written a function which should reference a couple of arrays, and return an array of the length of the longest value in the arrays. Confused, let me explain some more. Code: [Select] <?php function getLongest($records,$headers){ foreach ($headers as $fieldid){ foreach($records as $recordid){ $length[]=strlen(getContent($fieldid,$recordid)); } $result[$fieldid]=max($length); } return $result; } ?> I pass in 2 arrays, the first ($records) being references to the record id's from my DB, the second ($headers) is the column from my DB table where I pull the record info from. I have another function getContent which cross references these 2 values and gets the actual data (this works fine) I am trying to create an array of every content entry for each heading, then iterate through it and find the longest value. Then return an array of heading reference against longest content length. My content varies between 10 characters to a couple of hundred characters long, dependant on the column. I have approx 3000 entries in the DB. The results I'm getting are just not what I'd expect, i.e, I run the function against columns showing longtitude and latitude of locations, (expect the result to be approx 12 for the longest) but I get 38?!?! I've even tried using trim() with it incase there's any leading / trailing whitespace. Any thoughts? Hey I have a string that looks like the following: Quote top-php-tutorials-2.html I have a script that cycles through each page. The 2 in the quote above is the page number. How can I extract the number between the - and the .html and replace it with another number? I've tried Code: [Select] substr($engine->selectedcaturl, 0,-6).$v.".html"But then I realised this only works for numbers that are 1 digit long Any input would be appreciated How can I test if a string has a number at the end? i.e. smith1 instead of smith? I won't know how long the string is or whether there is more than 1 number but I will know that the string's last character is a number. Thank you! Hello, I'm working on an error form, and I want to check if the user inserted a number into the name field, and if so return an error. I thought this might of worked, but it doesn't: Code: [Select] if(is_int($_POST['name'])){ $arrErrors['name'] = 'Invalid name, remove numbers.'; } Many thanks I need to find first digit (any number 0 - 9) in a string. I thing I should be able to use: $NumberPosition = strpos($Source, "/[0-9]/", $NumberPosition+1); but it does not work, wonder what I am doing wrong? Hey guys, I have a url that looks like this http://www.mysite.com/our-company-t-2.html I need to extract the number at the end. It's 2 here but could be 2478 for example. Just wondering how I can do this. Would it need to do something like look for anything between "-" and ".html" ? I can't figure out how to pull the number from a form name Unfortunately I cant use substr(); because non of the names are the same length... these are the form names cat_sub_id_1 cat_name_1 cat_order_1 cat_sub_id_2 cat_name_2 cat_order_2 cat_sub_id_3 cat_name_3 cat_order_3 this is a list of the numbers that I need to be pulled out. Hiya! I have a file (see attached) that contains basic HTML for a page with multiple paragraphs. How do I check the length of each paragraph to find out if it is 30 characters or over? If the first paragraph is not equal to 30 characters, then it should move on to the next one, and so on. If a paragraph is found to be the correct length, I then need to extract the text from the <p> tags. If a none of the paragraph match 30 characters in length, then the code will need to choose the best length paragraph. Any help is greatly appreciated. Many Thanks, FishSword I've got a string which is usually abc-def-0123 but it sometimes has an additional dash and then a single digit (e.g. ghi-jkl-4567-1). It's always the same pattern and length, (three letters, a dash, three letters, a dash, 4 digits then the possibility of a dash followed by a single digit). What's the best way to test if the string ends in a dash followed by a single digit? Any help much appreciated! String: NodeJs is one of the best technology Quote
NodeJs is one of the best technology
How to print the largest even number? I have a large amount of text, (text can be different at any time), I count the number of characters in the string,and divide it by 2 so that half of the text will be in one variable, the other half in the other, however I am not sure how to get the split function, or whichever function to split it by the number of characters. example: $text = "lorem ispum dolar sit amet, consecterur adiscing elit. Donec dictum placerat sapien, in eliefend liber"; $cut = strlen($text)/2; //(here's where i cannot get it to work/do not know which function to use and how to use it) //but what I want it to do is to split it half way. I was wondering if anyone knows a good way to check for an even number of each character in a string. For example if the string is "abbacc" then it will return true as there are two of each character. However if the string is "ababc" it will return false as there is only 1 c. Thanks for any help I have a string in the form of "word1-word2-word3-word4-word5-word-6-word7", but I want to shorten it to 5 words only. How could I split a string in half but not in the middle of a word? $information = "This is the string of text, however at some point in the sentence a word may get split in half."; $cut = round(strlen($information)/2); ///where to cut (50%) $split1 = substr($information, 0, $cut); //first half $split2 = substr($information, $cut); // second half This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=353613.0
Hi all dear
<!DOCTYPE html> <html> <body> <?php function generatePermutation($string,$start,$end){ if($start == $end-1){ echo "$string - "; } else{ for($i = $start; $i < $end; $i++){ $temp = $string[$start]; $string[$start] = $string[$i]; $string[$i] = $temp; generatePermutation($string,$start+1,$end); $temp = $string[$start]; $string[$start] = $string[$i]; $string[$i] = $temp; } } } $str = "ABC"; $n = strlen($str); echo "All the permutations of the string a "; generatePermutation($str,0,$n); ?> </body> </html>
I'm getting the dreaded " Invalid parameter number: number of bound variables does not match number of tokens" error and I've looked at this for days. Here is what my table looks like:
| id | int(4) | NO | PRI | NULL | auto_increment | | user_id | int(4) | NO | | NULL | | | recipient | varchar(30) | NO | | NULL | | | subject | varchar(25) | YES | | NULL | | | cc_email | varchar(30) | YES | | NULL | | | reply | varchar(20) | YES | | NULL | | | location | varchar(50) | YES | | NULL | | | stationery | varchar(40) | YES | | NULL | | | ink_color | varchar(12) | YES | | NULL | | | fontchosen | varchar(30) | YES | | NULL | | | message | varchar(500) | NO | | NULL | | | attachment | varchar(40) | YES | | NULL | | | messageDate | datetime | YES | | NULL |Here are my params: $params = array( ':user_id' => $userid, ':recipient' => $this->message_vars['recipient'], ':subject' => $this->message_vars['subject'], ':cc_email' => $this->message_vars['cc_email'], ':reply' => $this->message_vars['reply'], ':location' => $this->message_vars['location'], ':stationery' => $this->message_vars['stationery'], ':ink_color' => $this->message_vars['ink_color'], ':fontchosen' => $this->message_vars['fontchosen'], ':message' => $messageInput, ':attachment' => $this->message_vars['attachment'], ':messageDate' => $date );Here is my sql: $sql = "INSERT INTO messages (user_id,recipient, subject, cc_email, reply, location,stationery, ink_color, fontchosen, message,attachment) VALUES( $userid, :recipient, :subject, :cc_email, :reply, :location, :stationery, :ink_color, :fontchosen, $messageInput, :attachment, $date);"; And lastly, here is how I am calling it: $dbh = parent::$dbh; $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); if (empty($dbh)) return false; $stmt = $dbh->prepare($sql); $stmt->execute($params) or die(print_r($stmt->errorInfo(), true)); if (!$stmt) { print_r($dbh->errorInfo()); }I know my userid is valid and and the date is set above (I've echo'd these out to make sure). Since the id is auto_increment, I do not put that in my sql (though I've tried that too), nor in my params (tried that too). What am I missing? I feel certain it is something small, but I have spent days checking commas, semi-colons and spelling. Can anyone see what I'm doing wrong? Hello,
I have problem durring binding update query. I can't find what is causing problem.
public function Update(Entry $e) { try { $query = "update entry set string = $e->string,delimiter=$e->delimiter where entryid= $e->id"; $stmt = $this->db->mysqli->prepare($query); $stmt->bind_param('ssi',$e->string,$e->delimiter,$e->id); $stmt->close(); } catch(Exception $ex) { print 'Error: ' .$ex->getMessage(); } }When I run function update I'm getting next error:Warning: mysqli_stmt::bind_param(): Number of variables doesn't match number of parameters in prepared statement Can you help me to solve this problem ? Edited by danchi, 17 October 2014 - 10:25 AM. I need to display a number(the number is retrieved from the db) in the form input field such that only the last 4 digits is visbile, the remaining can be masked as * or X or whatever is applicable. I know the last 4 can be obtained as follows: Code: [Select] $number=substr($number,-4,4); But when i hit the submit button the form validates the input field and checks if the number is a valid number of a specific format. Therefore when I click on the submit button then I should still be able to unmask the masked numbers or do something similar that would help me validate the whole number. Code: [Select] <input type="text" name="no" value="<?php if(!empty($number)){ echo $number;} ?>"> Hi, I am trying to make some adjustments to uploadify.php which comes with the latest version of uploadify (3.0 beta), so that it works with a session variable that stores the login username and adds it to the path for uploads. Here is uploadify.php as it currently looks: Code: [Select] <?php session_name("MyLogin"); session_start(); $targetFolder = '/songs/' . $_SESSION['name']; // Relative to the root if (!empty($_FILES)) { $tempFile = $_FILES['Filedata']['tmp_name']; $targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder; $targetFile = rtrim($targetPath,'/') .'/'. $_FILES['Filedata']['name']; // Validate the file type $fileTypes = array('m4a','mp3','flac','ogg'); // File extensions $fileParts = pathinfo($_FILES['Filedata']['name']); if (in_array($fileParts['extension'],$fileTypes)) { move_uploaded_file($tempFile,$targetFile); echo '1'; } else { echo 'Invalid file type.'; } } echo $targetFolder; ?> I added Code: [Select] echo $targetFolder; at the bottom so that I could make sure that the string returned was correct, and it is, i.e. '/songs/nick'. For some reason though, uploads are not going to the correct folder, i.e. the username folder, but instead are going to the parent folder 'songs'. The folder for username exists, with correct permissions, and when I manually enter Code: [Select] $targetFolder = '/songs/nick';all works fine. Which strikes me as rather strange. I have limited experience of using php, but wonder how if the correct string is returned by the session variable, the upload works differently than with the manually entered string. Any help would be much appreciated. It's the last issue with a website that was due to go live 2 days ago! Thanks, Nick |