-
Posts
14,780 -
Joined
-
Last visited
-
Days Won
43
Everything posted by .josh
-
shouldn't use ereg functions, as they will not be part of core build as of php6. if (!preg_match('~^\w*[0-9]\w*$~',$string,$match)) { // incorrect format } note about that regex: you didn't really specify what else was acceptable, beyond requiring at least 1 digit to appear somewhere in the $string. This pattern will match lowercase or uppercase letters, numbers or underscores (_). Pattern will match 0 or more of those, and at least 1 digit. In other words, if someone's name was "8" then it will return true. You didn't specify beyond the 1 digit thing, so there you have it.
-
[SOLVED] Filter out everything from string except keywords?
.josh replied to genu's topic in PHP Coding Help
If you keep the current [keyword] format (with the [..]) the stristr is faster than preg_match. Why? Because [] is significant in regex so you have to take extra steps to escape each of those brackets (look in function preg_method below). I have a preg2_method function that uses the same array but without brackets. Using preg_match_all without having to go through the extra steps pretty much evens it out with stristr method. So, if the keywords have to have the brackets around them, I'd stick with the stristr. If you can remove them, speedwise, they are about even. But, you don't have to worry about altering code if you decide to change up your keyword delimiters, whereas, you may or may not have to change it up with preg_match. So overall, I'd stick with the stristr method. <?php $keywords = array('[keyword1]', '[keyword2]', '[keyword3]'); $keywords2 = array('keyword1', 'keyword2', 'keyword3'); $string = '<h1><strong>[keyword1]</strong></h1><br /> not keyword [keyword2], or keyword <strong>[keyword3]</strong>'; function preg_method($keywords, $string) { $keywords = preg_quote(implode(' ',$keywords)); $keywords = explode(' ',$keywords); $keywords = implode('|',$keywords); preg_match_all("~$keywords~",$string,$matches); } function preg2_method($keywords, $string) { $keywords = implode('|',$keywords); preg_match_all("~$keywords~",$string,$matches); } function stristr_method($keywords, $string) { foreach($keywords as $word) { if (stristr($string,$word) !== false) $list[] = $word; } } echo "preg_match_all method: <br/>"; for($x = 1; $x <= 10; $x++) { $start = (float) microtime(true); preg_method($keywords, $string); $time = (float) microtime(true) - $start; echo "$time<br/>"; } echo "preg2 method: <br/>"; for($x = 1; $x <= 10; $x++) { $start = (float) microtime(true); preg2_method($keywords2, $string); $time = (float) microtime(true) - $start; echo "$time<br/>"; } echo "stristr method: <br/>"; for($x = 1; $x <= 10; $x++) { $start = (float) microtime(true); stristr_method($keywords, $string); $time = (float) microtime(true) - $start; echo "$time<br/>"; } ?> output: -
[SOLVED] Filter out everything from string except keywords?
.josh replied to genu's topic in PHP Coding Help
stristr (case insensitive) or strstr (case sensitive) is the fastest way to check if one string is in another. As far as looping through each word...well, you have a list of words, so yes, you're going to have to loop through each one. I suppose you could do a preg_match_all with alternation but I don't know if that would necessarily be faster. -
[SOLVED] Filter out everything from string except keywords?
.josh replied to genu's topic in PHP Coding Help
$keywords = array('[keyword1]', '[keyword2]', '[keyword3]'); $string = '<h1><strong>[keyword1]</strong></h1><br /> not keyword [keyword2], or keyword <strong>[keyword3]</strong>'; foreach($keywords as $word) { if (stristr($string,$word) !== false) // use strstr if you want case sensitivity $list[] = $word; } print_r($list); -
If you are validating the strings with those regexes, returning info whether it passes or not, and then sending another request to the server to query the db with the info, you are still open for attack. Why? Because you validated the info for that first request. But you are sending a different request when you are querying the database. Someone could just bypass this validation altogether. The db query should be in the same script/request as your validation.
-
$row will be an array of each column for the current row you are on. So echo $row['blah']; would echo out the column named 'blah'. So for example, if you have the following db table: sometable id name 1 Bob 2 Jan 3 James you can for instance, echo out each column in the current row like this: $sql = "select * from sometable"; $result = mysql_query($sql); while ($row = mysql_fetch_assoc($result)) { echo "id: {$row['id']} name: {$row['name']} <br/>"; } This would be the output: id: 1 name: Bob id: 2 name: Jan id: 3 name: James So basically you would do $row['columnname'] if you are using _fetch_array or _fetch_assoc and you would refer to them individually, or you can loop through each one, inside your while loop: while ($row = mysql_fetch_assoc($result)) { foreach ($row as $column => $value) { echo "$column : $value <br/>"; } echo "<br/>"; }
-
http://www.phpfreaks.com/tutorial/php-basic-database-handling
-
[tex]\widetilde{\underline{\zeta\Gamma\alpha\gamma\sigma}}\sqrt[n]{\underline{|\phi}\int\underline{\Sigma\eta\tau}}[/tex] That's how I roll.
-
[tex]1+1=2[/tex] fear my skills.
-
Column count doesn't match value count at row 1
.josh replied to chris_s_22's topic in PHP Coding Help
Don't really know what you're going on about the title, but I fixed some errors in your code. <?php function create_game($playername, $sex) { if($_POST) { foreach($_POST['player'] as $post_key) { $query_string .= " ('".$post_key['sex']."', '".$post_key['playername']."'),"; } $query_string = substr_replace($query_string,"",-1); // store the information in the database $query = "INSERT INTO `game` (`playername`, `sex`) values $query_string"; $result = mysql_query($query) or die(mysql_error()); // if suceesfully inserted data into database, send confirmation link to email if($result) { header('Location: http://www.pinkangel4u.com/truthdare/Game/gameboard.php'); } }} ?> -
As mentioned, solved threads become dead threads, so they'll be bumped off anyways. I would support solved threads being moved to their own sub-forum if I thought people would actually search through them before posting their question, but I know that that won't happen any more than people searching in the first place. Or reading stickies. Or going to the FAQ forum. Which is practically never.
-
If you are just starting out and don't know what you're doing, what do you expect to do that wouldn't be included in a beginner's kit anyways? Buy parts individually and read some tutorial online? In essence, it's the same thing, except with a prefab kit, you're guaranteed not to accidentally buy the wrong parts, due to not knowing what you're doing.
-
probably because you are using an assignment operator instead of an equality operator.
-
$_SERVER['REMOTE_ADDR'];
-
If you insist, you could always do something like this: $array = array('a','b','c','d','e'); $c = count($array); for ($x = 1; $x <= $c; $x++) { $tarray[$x] = $array[$x-1]; } $array = $tarray; Though I'd personally just do the ..-1 thing already mentioned.
-
'unique' visitors without login are always an estimation. You can start a session that tracks the IP for the duration of the session. Simply refreshing the browser won't end the session, so you can continue to track the IP based on that. Don't increment page view as long as session is active and IP matches on stored in session var. You can also attempt to set a cookie. If client accepts cookies, you can set one and check for it. Don't increment if cookie is found. Neither of those things are 100% insurance that user is unique. Cookies can be disallowed. Someone could be going through a proxy. So, the only way to ensure unique views is to require user to register/login.
-
lose 1 customer or lose 100 products. Take your pick.
-
only way is to require login to view.
-
stuff has to be purchased, right? Require a bunch of personal info on register. A cc number (for purchasing), social security number, bday, address, etc... upon login, prompt user to verify him/herself with the more sensitive data, like their social security number or cc number. Not many people are willing to share that kind of info, even with their friends.
-
You'd be surprised how often and how long businesses stick to the same thing.
-
There is a war going on right now: a war on terrorism. RAH!