-
Posts
1,832 -
Joined
-
Last visited
-
Days Won
3
Everything posted by salathe
-
First, you're using the wrong type of quotation marks (double instead of single) in that snippet. Second, it is advisable to use preg_quote because the variable value might contain special characters (like yours contains the dot) which mean something in regular expressions. An example might be: $img = "fun.jpg"; preg_match_all('~[\'"](.*?)' . preg_quote($img, '~') . '[\'"]~is', $str, $a); I sometimes like to make things a little "neater" (arguably) using sprintf but it's purely cosmetic and does nothing different than the code above: $img = "fun.jpg"; $pattern = sprintf('~[\'"](.*?)%s[\'"]~is', preg_quote($img, '~')); preg_match_all($pattern, $str, $a);
-
Perhaps this function may help, it looks like it might.
-
Is it literally that text? I only ask because it's an odd format to provide data in (for consumption).
-
You could also make use of the SPL, in particular the SplFileObject to get at the colon-separated-data in an object-oriented way. Here's an example which should (in theory) be pretty straightforward to understand. $users = new SplFileObject('users.txt'); $classes = new SplFileObject('class.txt'); foreach (array($users, $classes) as $file) { $file->setFlags(SplFileObject::READ_CSV); $file->setCsvControl(':'); $file->headings = $file->current(); } // Get a list of students foreach ($users as $user) { $user = array_combine($users->headings, $user); // Collect only students if ($user['Status'] === '0') { $students[$user['Email']] = $user; } } // Iterate over classes foreach ($classes as $class) { $class = array_combine($classes->headings, $class); // Remove from students list if (isset($students[$class['Email']])) { unset($students[$class['Email']]); } } print_r($students);
-
counting specific words in string (reliably)
salathe replied to gerkintrigg's topic in PHP Coding Help
Or, just look at the return value from preg_match_all which is the number of matches found. -
First let me start by saying that $_POST['name'] is not a multidimensional array (though $_POST in your case is). As already mentioned above, the value of $_POST['name'] when no input boxes have been filled in is an array of empty strings: in plain PHP code it would look like $_POST['name'] = array('', '', '', '', '', '', '', '', '', ''); — an array containing 10 items which are all empty strings. Now, lets look at empty. It will return TRUE if an "empty array" is provided. An empty array is one which contains no items — array() Can you see why yours is different to an empty array? As for some form of solution to your problem. There are generally multiple ways to achieve the same result in PHP and here's one: $names = array_filter($_POST['name'], 'strlen'); if (empty($names)) { // No names were submitted } This loops over all of the values in $_POST['name'] and if the value has any string length (ie. there is something there) it will get added on to the $names array. Conversely, to help clarify, if any of the items in $_POST['name'] have no string length (they weren't filled in in the form) then they won't get added to the $names array. Then we can simply check if the $names array has any values to see if there were any names submitted.
-
*salathe likes the double-entendre Back on topic, maybe I missed it but I've seen no AIR posts.
-
It would probably be easier and more correct to make proper use of the recursible nature of the RecursiveDirectoryIterator; it would save manually recursing the grab_files function and a lot of array merging. Here is a version the same function with a couple of differences: 1. Recurses using the iterator rather than by calling the function multiple times 2. Does not need array_merge 3. Allows multiple file extensions and checks them case insensitively The method of getting random keys is different too; instead of shuffling the entire array (which might be huge) it uses array_rand function grab_files($start_dir, $ext) { $dir = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($start_dir)); $files = array(); // Force array of extensions and make them all lower-case if ( ! is_array($ext)) { $ext = (array) $ext; } $ext = array_unique(array_map('strtolower', $ext)); foreach($dir as $file) { // Skip anything that isn't a file if ( ! $file->isFile()) continue; // If the file has one of our desired extensions, add it to files array if (in_array(strtolower(pathinfo($file->getFilename(), PATHINFO_EXTENSION)), $ext)) { $files[] = $file->getPathname(); } } return $files; } // Grab all .jpg files recursively $files = grab_files('./gallery', 'jpg'); // Get random keys $keys = array_rand($files, min(20, count($files))); foreach($keys as $key) { echo $files[$key] . "<br>\n"; }
-
In future, please either post the source of the function (in the thread, in a pastebin or as a link to it: e.g. http://www.phpfreaks.com/forums/index.php?topic=278778.0) so that we can see how it works, since it's not a core PHP function. For this particular issue (accepting an array of allowable extensions) the line reading if(pathinfo($file, PATHINFO_EXTENSION) == $ext) inside the grab_files function will need to be amended to compare the file extension against the provided array.
-
In short, no.
-
Apache performance - serving image resources from subdomain?
salathe replied to random1's topic in Apache HTTP Server
The short answers would be, "it's likely" and "no". Here is a valuable link to get you started: http://developer.yahoo.com/performance/rules.html -
Are you using exactly that code? If so, that makes no sense at all since the regex looks for a word but you only supply it with that word in the subject parameter and provide a string for what should be a by-ref variable. From the looks of things, you probably want more like: $plus = preg_match_all('/\b'.preg_quote($r['word'], '/').'\b/', strip_tags($my_page));
-
$GPRMC and NMEA how to extract from report
salathe replied to cr-ispinternet's topic in PHP Coding Help
You could use a basic regular expression to grab only the $GPMRC line (are you familiar at all with regular expressions?) then break that down into an array (or whatever) to work with. Here's a quick example (only lines 1, 2 and 4 are important). preg_match('/^\$GPRMC,.*$/m', $read, $match); $line = $match[0]; $keys = explode(',', 'command,timestamp,fix,lat,lath,lon,lonh,knots,bearing,date,mag_var,mode,checksum'); $values = explode(',', $line); $recmin = array_combine($keys, $values); // Just output the array to test var_dump($recmin); -
Use SimpleXMLElement::asXML with a filename. $request->asXML("data.xml");
-
You could use preg_match_all which returns the number of matches for the regexp provided to it (ie., a value for $plus).
-
You could also use the array_fill function to create an array of however many of the URLs you want. $urls = array_fill(0, $_GET['number'], $_GET['url']);
-
What happens if you use the following file? <?php $sxml = new SimpleXMLElement('http://www.zideo.nl/media.php?playzideo=6b3447616f467478&zideo=6c49575a6e6c633d', NULL, TRUE); header('Content-Type: application/xml'); echo $sxml->saveXML(); ?>
-
What do you get if you have a PHP page containing the following: <?php ini_set('display_errors', 'On'); error_reporting(E_ALL); header("Content-Type: text/plain"); echo file_get_contents("http://www.zideo.nl/media.php?playzideo=6b3447616f467478&zideo=6c49575a6e6c633d"); ?>
-
$milliseconds = 1000 * strtotime('25-11-2009');
-
I'm all for giving assholes a hard time (read into that what you will) and it certainly wasn't my intention to suggest that the staff here should put up with any abuse from said assholes. My main point was simply that someone had a genuine issue with something the staff did and the overwhelming response was "you're an asshole, go away". If that's not anything of concern to you guys, fair enough. I'm getting a clearer picture of the way things are done around here as time goes by, both for participating in the community and for seeing how the place is looked after by the staff (and concerned citizens). I'm sure the system works, and works well, for this site but the persistent air of superiority around many staff members' posts doesn't make me feel comfortable at all, clashing with the otherwise great community feel. It may mostly just be banter within the clique put across in a public format, I don't know. Then again, who gives a crap about what I think.
-
I am not clear how the OPs posts in this thread could be considered as "attacking staff" though perhaps that refers to some other incident between him and staff that I'm not privy to: the ramblings of a disgruntled member sure, but hardly an attack. Maybe my own experience with managing members of a forum (and managing staff behaviour) has had an effect on how I would react to situations like those presented in this thread, it's a possibility. However, when someone has an issue (however politely or crudely it is put across) with something, I listen and take the time to respond thoughtfully rather than jump in with the first response (which I agree might often be to defend oneself) that comes to mind. If someone were to say, "[the] PHP manual is crappy, whoever is working on it is a bunch of idiots" then I certainly wouldn't feel any need to tell them to stop using the manual. The manual does have a lot of work still to be done (and always will), the point is very valid and I'm sure the volunteers working on it would let the idiot remark roll off of them like water off a duck's back. In all seriousness a quick rant is nothing to get all concerned about. After all, we all volunteered to improve the manual in the first place, so we must be aware there are "crappy" parts that need to be completed and/or fixed! Sure, it would be nice if everyone took a more diplomatic stance when airing their grievances but just because a viewpoint is put across crudely doesn't make it any less valid, does it? I believe there's no one right answer to that. In the end, it seems that the OPs issue is simply that staff didn't contact him about deleting a thread (threads?). Not such a big issue and hardly a big change to policy to ask staff to inform members of why their threads were deleted (whether in an automated manner, via PM or whatever), if that isn't generally done already. One must simply be prepared that some members will on occasion feel the urge to take issue with staff actions and post publicly about it due to the lack of any more private dialog beforehand. How the staff reacts publicly to such an outburst will say infinitely more to the community at large than any private discussions between staff and members. P.S. Sorry Justin L H for perhaps taking your thread off on a tangent. Perhaps my posts should be wrapped in [ot] tags.
-
Since you're only looking to check if the value contains letters, you could also use the ctype_alpha function.
-
If as the OP said, it's simply an issue of not having an edit button then why not just address that rather than going off on some staff-are-volunteers-you-agreed-to-the-rules replies? The replies to this thread appear to fall under the categories of: 1. Do something which is generally frowned upon in web forum etiquette. (double-post) 2. Staff are volunteers. (so?) 3. Piss off. Forgive me for stepping on anyone's toes but it appears (to a visitor, not in-the-know, entirely based on this thread) the general sentiment in this case is you're an idiot, go away. Nice. Maybe it's because I'm still fairly new here myself so haven't really fallen into the clique (may be seeing things too plainly) yet but that sentiment seems non-conducive to a fruitful and healthy staff-visitor relationship. Are the sort of responses posted in this thread common responses to any user with a gripe?
-
Change the (.*?) to (\d+) The reason is because your capturing group (that which needs changed) was grabbing more than you want it to. For example, here's your string with the original matches highlighted: Key: no match, match, group 1. [pre] ......<a href="/users/blog.php?user=ted_chou12&id=48" class="readmore">Read more</a> <a href="/users/blog.php?user=ted_chou12&id=48#comments" class="comments">Comments (0)</a> ....<a href="/users/blog.php?user=ted_chou12&id=47" class="readmore">Read more</a> <a href="/users/blog.php?user=ted_chou12&id=47#comments" class="comments">Comments (0)</a>... <a href="/users/blog.php?user=ted_chou12&id=46" class="readmore">Read more</a> <a href="/users/blog.php?user=ted_chou12&id=46#comments" class="comments">Comments (0)</a>.... [/pre] Compare that with using (\d+): [pre] ......<a href="/users/blog.php?user=ted_chou12&id=48" class="readmore">Read more</a> <a href="/users/blog.php?user=ted_chou12&id=48#comments" class="comments">Comments (0)</a> ....<a href="/users/blog.php?user=ted_chou12&id=47" class="readmore">Read more</a> <a href="/users/blog.php?user=ted_chou12&id=47#comments" class="comments">Comments (0)</a>... <a href="/users/blog.php?user=ted_chou12&id=46" class="readmore">Read more</a> <a href="/users/blog.php?user=ted_chou12&id=46#comments" class="comments">Comments (0)</a>.... [/pre]
-
Show us the code generating that result.