-
Posts
20 -
Joined
-
Last visited
Everything posted by dde
-
Interesting webpage Jacques, I found this article on it regarding PDO (and more).
-
Ch0cu3r, could you explain me what the question mark and the plus sign is for? php.net manual says: ? extends the meaning of (, also 0 or 1 quantifier, also makes greedy quantifiers lazy. +1 or more quantifier. I'm lost.
-
As ginerjm posted PDO is an option as well. Personally I'm also new to mysqli so I'm learning it as we speak. Anyway here is some small example: Connecting to a database is almost the same instead of msql_connect, you will now use msqli_connect or new mysqli. Mysqli stands for mysql improved. /* connection is set in the $connection variable */ $connection = new mysqli($db_server, $db_user, $db_pass) or die(mysqli_error()); /* OR with mysqli_connect. Note that you either use msqli_connect or new mysqli */ $connection = mysqli_connect($db_server, $db_user, $db_pass) or die (mysqli_error()); /* We can now connect and select the database with */ mysqli_select_db($connection, $db_name) or die (mysqli_error()); /* We we can use the mysqli prepared or PDO to safely insert data in the database. */ /* We prepare the inserting statement."image_author, image_description and image_pathfile */ /* are the Database column names. */ $statement = $connection->prepare("INSERT INTO images (image_author, image_description, image_pathname) VALUES (?, ?, ?)"); /* We can now bind the values which are supposed to go into these */ /* colums, stated as questionmarks. using the bind_param function. */ /* We also declare whether the values are a double (d), string (s) */ /* int (i) or blob (b)or blob with the i, d, s or b at the start */ /* of the blind_param. */ $statement->bind_param("sss", $image_author, $image_description, $image_pathname); /* Now execute the prepared statement */ $statement->execute();
-
I have also tried the following, but not sure what to do with the result. preg_match_all("/\[(.*?)\]/", $text, $matches); $array = array(); for($i=0; $i < count($matches[1]); $i++){ $split = explode(" ", $matches[1][$i]); foreach ($split as $value){ array_push($array, $value); } }
-
I'm looking for a method to replace everything between brackets. The patterns that I'm looking for are defined: "/\[([2-9TJQKA][hdcs])\]/" The pattern inside the bracket could accur 2 or even 3 times inside the brackets. For example first occurance would be [Ac Kd], here I want to replace Ac into an imge and Kd too. Later in the text I will most of the time get 3 sets, instead of two: [Jc Td 9d]. The sets always consist of [2-9JQKA][hdcs] and also in this order. Another example: [2d 2s 2h], or [3s 4h 5c]. If I use the following function, it would not work optimally because some texts do contain some of those sets which should not be converted, ONLY the sets between the brackets: $haystack = setReplace($haystack); function setReplace($haystack) { $array = array("2s", "2h", "2d", "2c", "3s", "3h", "3d", "3c", "4s", "4h", "4d", "4c", "5s", "5h", "5d", "5c", "6s", "6h", "6d", "6c", "7s", "7h", "7d", "7c", "8s", "8h", "8d", "8c", "9s", "9h", "9d", "9c", "Ts", "Th", "Td", "Tc", "Js", "Jh", "Jd", "Jc", "Qs", "Qh", "Qd", "Qc", "Ks", "Kh", "Kd", "Kc", "As", "Ah", "Ad", "Ac"); for($i=0;$i < count($array);$i++) { $result = str_replace($array[$i], "<img src=\"/images/".$code[$i].".png\">",$haystack); } return $result; } Any ideas?
-
I took the substr and strpos method. /* starting position of the text */ $startpos = 0; /* string to find */ $string = "string"; /* count the string length */ $addcount = strlen($string); /* find the position of the string in the user submitted haystack */ $stringpos = strpos($haystack, $string); /* specify the position to cut the haystack */ $endpos = $stringpos + $addcount; /* put the first portion of the haystack in a vriable to be printed out later */ $header = substr($text, $startpos, $endpos); /* I always use var_dump to check for my results */ var_dump($firstPortion); /* Next portion of the haystack to be cut */ $string = "string"; /* count the previous $header string length */ $addcount = strlen($firstPortion); /* find string position for second portion of haystack, starting where the previous cut ended (strlen($firstPortion)). */ $stringpos = strpos($text, $string); $stringpos = $stringpos - $endpos; /* put the second portion in a variable */ $secondPortion = substr($text, $addcount, $stringpos); /* check for results */ var_dump($secondPortion); Works fine, and as expected. I'm however not satisfied with the coding itself. Any ideas for shorter and more cleaner methods?
-
So far I've been thinking about using strpos, to find the position to cut. Then use substr to cut it out and put that portion in a variable.
-
I'd like to edit specific parts of a submitted text what is the best way to do this? For example I get the following text: [name country] is very cold this time of year. Because I like the cold I would love to live there [end of line 2] whole lot more text here until [name country] if this text here exists, blabla [but Germany] is blabla [end of line] in some cases some more text here [summary] bla bla The text in the brackets are words that I already know before it has been submitted. Getting the words out has been part of my previous script, using preg_match and put them in variables. I want to put the first portion of the text in a row, if 2nd, 3rd and 4th portion exist, put them in a row too. Any ideas? [edited] I prefer to have these portions cut out and put in a variable. So I end up having a few variables and can later echo that out in rows
-
This is because the variable $mailtoname and $mailtoemail are blank. When you want to get the values of form values, you have to use $_POST. In your form this is also states as "method=post". To get values from a form, you have to send it to the receiving page, in this case it is "sendemailWithCC_BCC.php". On the receiving page use the $_POST variable to get the values from the form.To do this you use $_POST['name of form input'] In your case you use $mailtoname = $_POST['mailtoname']; To see if it works just use echo $_POST['mailtoname'];
-
Where is this basicForm.php located? Are you running it on a server supporting php? The code itself is working. Note though that there is a typo <form name"form1" method="POST" action="basicForm.php"> This however is not the cause of your problem.
-
preg_match("/[\$\€]\d+[.]\d+\/[\$\€]\d+[.]\d+/", $haystack, $matches); Matches €00.00/€00.00 as well as $00.00/$00.00.
-
Okay I'm done with searching for answers, working on this preg_match for about 3 hours now. I'm looking for "($0.01/$0.02 USD)" in a string. The needle might be slighty different. Possible strings that I might look for is: (€xx.xx/€xx.xx EUR) where the EUR can be changed into USD, including its signs.
-
It would not be possible to have patterns in an array?
-
So far I got this, but when having lots of words, the code becomes too big. Is it possible to have the words in an array? if(preg_match("/java/", $text)) { $match = "java"; } elseif(preg_match("/PHP/", $text)) { $match = "PHP"; } elseif(preg_match("/html/", $text)) { $match = "html"; } else { $match = "unknown programming language"; } echo $match;
-
I'm trying to preg_match $text for specific words, defined in an array. $text = "PHP is a programming language of choice"; $text = "This is a function written in a programming language called PHP."; $words = array("/java/", "/PHP/", "/html/"); for ($i = 0; $i < count($words); $i++) { preg_match($words[$i], $text, $matches); } The problem with tis is that it returns 3 arrays, of which only the second one is a match, and the other 2 arrays are empty. The result is that I get notice error on my echo page because the 1st and 3rd arrays are empty. if I want the text to be searched for the first matching result, whether it is java, PHP or html, then stop (if possible) and echo/return the result, how will I do that? Perhaps preg_match is not to be used here but instead something like substr?
-
I can't get the min and max range for the FILTER_VALIDATE_INT to work. $subYoB = $_POST['yob']; $valYoB = array('option' => array('min_range' => 1900, 'max_range' => 2000)); if(!filter_var($subYoB, FILTER_VALIDATE_INT, $valYoB)) { echo "Not valid"; } else { echo "Valid"; } Whenever the input of $_POST['yob'] is an INT it validates even when the input is lower the min_range, and higher than the max_range. What am I doing wrong? I also tried this, without succes. if(!filter_var($subYoB, FILTER_VALIDATE_INT, array('option' => array('min_range' => 1900, 'max_range' => 2000))))
- 3 replies
-
- filter_var
- filter_validate
-
(and 1 more)
Tagged with:
-
Sometimes when I need a specific class. The session class is global in most cases.
-
I found the problem. The "function startSession()" is not working the way I want it to work. When I replace everything inside this function with "session_start();" it all works. So what is wrong with my startSession function?
-
Aren't these SESSION values been set in the process.php? $_SESSION['value_array'] = $_POST; $_SESSION['error_array'] = $form->getErrorArray(); header("Location: index.php");
-
I'm trying to rewrite the old jpmaster77 login script that I used back in the day quite extensively, but I'm having problems getting the $form->error messages to the correct page. Login form, on the index.php: <div> <h3>Login</h3> <form method="post" action="process.php" id="sublogin"> /* error message should be displayed here when username is empty or == username */ <p> <?php echo $form->error("user"); ?> <input class="loginside" type="text" name="user" value="username" /> ?> <br /> <input class="loginside" type="password" name="pass" value="password" /> <br /> <input type="hidden" name="sublogin" value="1"> <input type="checkbox" name="remember">Remember me <br /> <input class="sublogin" type="submit" value="login" style="border: 0; margin: 5px 0 0 1px;" /> </p> </form> Not registered yet? Sign up here! </div> process.php class Process { /* Class constructor */ function Process(){ global $session; /* User submitted login form */ if(isset($_POST['sublogin'])){ $this->procLogin(); } /** * Should not get here, which means user is viewing this page * by mistake and therefore is redirected. */ else{ header("Location: index.php"); } } function procLogin(){ global $session, $form; /* Login attempt */ $_POST = $session->cleanInput($_POST); $retval = $session->login($_POST['user'], $_POST['pass'], isset($_POST['remember'])); if($retval){ header("Location: correct.php"); } else{ $_SESSION['value_array'] = $_POST; $_SESSION['error_array'] = $form->getErrorArray(); header("Location: index.php"); } /* if instead the $_SESSION and header I enter echo $form->error("user") then the error message is displayed in process.php, and not in index.php */ } } $process = new Process; ?> But when instead the $_SESSION and the header location I enter here echo $form->error("user"); then all is fine. But the error message should not be displayed in the process.php, but rather above the login form on the index.php session.php <?php include "database.php"; include "form.php"; class Session { var $time; function Session(){ $this->time = time(); $this->startSession(); } function startSession(){ $session_name = 'sec_session_id'; // Set a custom session name $secure = true; // This stops JavaScript being able to access the session id. $httponly = true; // Forces sessions to only use cookies. if (ini_set('session.use_only_cookies', 1) === FALSE) { header("Location: ../error.php?err=Could not initiate a safe session (ini_set)"); exit(); } // Gets current cookies params. $cookieParams = session_get_cookie_params(); session_set_cookie_params($cookieParams["lifetime"], $cookieParams["path"], $cookieParams["domain"], $secure, $httponly); // Sets the session name to the one set above. session_name($session_name); session_start(); // Start the PHP session session_regenerate_id(true); // regenerated the session, delete the old one. } function login($subuser, $subpass, $subremember) { global $database, $form; /* username check */ $field = "user"; if(!$subuser || $subuser == "username") { $form->setError($field, "* Username incorrect"); } /* password check */ /* if e-mail welcome, check if user activated account */ /* Return if form errors exist */ if($form->num_errors > 0){ return false; } return true; } function cleanInput($post = array()) { foreach($post as $k => $v){ $post[$k] = trim(htmlspecialchars($v)); } return $post; } }; $session = new Session; $form = new Form; ?> form.php <?php /** * Form.php * * The Form class is meant to simplify the task of keeping * track of errors in user submitted forms and the form * field values that were entered correctly. * * Written by: Jpmaster77 a.k.a. The Grandmaster of C++ (GMC) * Last Updated: August 19, 2004 */ class Form { var $values = array(); //Holds submitted form field values var $errors = array(); //Holds submitted form error messages var $num_errors; //The number of errors in submitted form /* Class constructor */ function Form(){ /** * Get form value and error arrays, used when there * is an error with a user-submitted form. */ if(isset($_SESSION['value_array']) && isset($_SESSION['error_array'])){ $this->values = $_SESSION['value_array']; $this->errors = $_SESSION['error_array']; $this->num_errors = count($this->errors); unset($_SESSION['value_array']); unset($_SESSION['error_array']); } else{ $this->num_errors = 0; } } /** * setValue - Records the value typed into the given * form field by the user. */ function setValue($field, $value){ $this->values[$field] = $value; } /** * setError - Records new form error given the form * field name and the error message attached to it. */ function setError($field, $errmsg){ $this->errors[$field] = $errmsg; $this->num_errors = count($this->errors); } /** * value - Returns the value attached to the given * field, if none exists, the empty string is returned. */ function value($field){ if(array_key_exists($field,$this->values)){ return htmlspecialchars(stripslashes($this->values[$field])); }else{ return ""; } } /** * error - Returns the error message attached to the * given field, if none exists, the empty string is returned. */ function error($field){ if(array_key_exists($field,$this->errors)){ return "<font size=\"2\" color=\"#ff0000\">".$this->errors[$field]."</font>"; }else{ return ""; } } /* getErrorArray - Returns the array of error messages */ function getErrorArray(){ return $this->errors; } }; ?> Does anyone know why the $form->error("user") variable cannot be transfered to the index.php? Is something wrong with the session_start or something? thanks in advance guys!