Re: [PHP] Association Problem?
On Mon, Jun 21, 2004 at 12:09:50PM -0400, Gabe wrote: Wanted to see if anyone had any input on this. I have a recordset that is returned to me in my script. What I then would like to do is go through each row of the recordset and use the substr_count function to count the number of times a string appears in each row. Then I'd like to sort display the recordset based on how many times the string appeared in each row. Basically this is a very light search with a very quick dirty way to find relevancy. I've thought that I could just store the relevancy numbers in an array, but then how do I know which relevancy number goes with what row in the recordset? That's the association problem I'm talking about. Also, how would I then sort the recordset accordingly based on the relevancy array? I've been looking through the PHP documentation and can't quite get past this question. Anyone have any ideas? Thanks! try array_multisort(): $search_str = 'abc'; $sort_array = array(); foreach ($recordset as $k = $v) { $sort_array[$k] = substr_count($v, $search_str); } array_multisort($sort_array, $recordset); - rob -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regular expression question
On Thu, May 27, 2004 at 09:59:05AM -0700, Dan Phiffer wrote: So I'm trying to implement a simple wiki-like syntax for hyperlinking. Basically I want to match stuff like [this], where the word 'this' gets turned into a hyperlink. I have that working, but I want to be able to escape the opening bracket, so that it's possible to do \[that] without having it match as a link. Here's what I've got: // Matches fine, but without escaping $pattern = / \[ # Open bracket ([^\]]+?) # Text, including whitespace \] # Close bracket /x ; // Throws an unmatched bracket warning $pattern = / [^\\] # Don't match if a backslash precedes \[ # Open bracket ([^\]]+?) # Text, including whitespace \] # Close bracket /x ; // Ignores escaping: \[example] still matches $pattern = / [^\\\] # Don't match if a backslash precedes \[ # Open bracket ([^\]]+?) # Text, including whitespace \] # Close bracket /x ; Nothing seems to change if I keep adding backslashes to that first matching thingy (i.e. the escaping still doesn't work). Any ideas? Try negative lookbehinds... $pattern = ' / (?!) \[# open [ not preceded by a backslash (.*?) # ungreedy match anything (?!) \]# close ] not preceded by a backslash /x '; - Rob -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: strip comments from HTML?
On Thu, May 06, 2004 at 11:48:36PM -0400, Paul Chvostek wrote: On Thu, May 06, 2004 at 07:11:55PM +, Curt Zirzow wrote: $text=one !--bleh\nblarg - two\n; print ereg_replace(!--([^-][^-]?[^]?)*--, ,$text); Because your missing a - $text=one !--bleh\nblarg -- two\n; /me applies mallet to head % php -r '$text=one !--bleh\nblarg -- two\n; print ereg_replace(!--([^-][^-]?[^]?)*--, ,$text);' one two whee, it works! :) you're still missing things like ! START -... don't know how you can get around that with ereg. also preg_replace('/!--.*?--/s', ...) is much faster. :-) - rob -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: strip comments from HTML?
On Thu, May 06, 2004 at 11:10:17AM -0400, Paul Chvostek wrote: On Thu, May 06, 2004 at 03:02:16PM +1000, Justin French wrote: This isn't working: $text = preg_replace('/!--(.*)--/','',$text); Can someone advise what characters I need to escape, or whatever to get it going? It's not a matter of escaping. You're matching too much with the .*. If you're sure you won't have any right-point-brackets inside comments, you can use something like: $text = ereg_replace(!--[^]*--,,$text); Accurately matching comments in an extended regular expression is tricky though. The only thing you can really *negate* in an ereg is a range, not an atom. And the close of the comment can't be prepresented as a range, since it's multiple characters. Not to say it can't be done. I just can't think of how at the moment. you can make the .* less greedy... $text = preg_replace('/!--.*?--/', '', $text); - rob -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: strip comments from HTML?
On Thu, May 06, 2004 at 12:47:10PM -0400, Paul Chvostek wrote: On Thu, May 06, 2004 at 11:26:34AM -0400, Rob Ellis wrote: $text = ereg_replace(!--[^]*--,,$text); you can make the .* less greedy... $text = preg_replace('/!--.*?--/', '', $text); Interesting to know. My preg-foo is limited; I came at PHP from a background of awk and sed, so when I regexp, I'm a little more traditional about it. Interestingly, from a shell: $ text='one !-- bleh -- two\nthree !-- blarg --four\n' $ printf $text | sed -E 's/!--([^-][^-]?[^]?)*--//g' one two three four which is the same behaviour as PHP. But that still doesn't cover multi-line. PHP's ereg support is supposed to, but doesn't work with this particular substitution: $text=one !--bleh\nblarg - two\n; print ereg_replace(!--([^-][^-]?[^]?)*--, ,$text); returns one !--bleh blarg - two But we know it really does support multiline, because: $text=bb\nbb; print ereg_replace([^ac],,$text); returns So ... this is interesting, and perhaps I'll investigate it further if the spirit moves me. ;-) right, to strip multi-line comments with preg_replace you need /s $text = preg_replace('/!--.*?--/s', '', $text); - rob -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] A to Z incremental
On Fri, Apr 23, 2004 at 09:41:39PM +1000, electroteque wrote: use the chr($i) system or range()... print implode(' | ', range('A', 'Z')); - rob -Original Message- From: Ford, Mike [LSS] [mailto:[EMAIL PROTECTED] Sent: Friday, April 23, 2004 9:31 PM To: 'Paul'; [EMAIL PROTECTED] Subject: RE: [PHP] A to Z incremental On 22 April 2004 15:22, Paul wrote: Hi! Got this script: ?php for($i='A';$i='Z';$i++){ echo $i.' | '; } The output is: A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P Q | R | S | T | U | V | W | X | Y | Z | AA | AB | AC | ... YX | YY | YZ | where is should display only letters from A to Z. Why is that? Because 'Z'++ (if you see what I mean!) is 'AA'. (So, at the end of the loop iteration which echoes 'Z', $i becomes 'AA', which is 'Z', and as a result the loop continues on through all the values you saw. The loop terminates after 'YZ' is echoed, since at the end of that iteration $i increments to 'ZA', which is not 'Z', and Bob's your uncle!) One way of solving this is: for ($i='A'; $i!='AA'; $i++) Cheers! Mike - Mike Ford, Electronic Information Services Adviser, Learning Support Services, Learning Information Services, JG125, James Graham Building, Leeds Metropolitan University, Beckett Park, LEEDS, LS6 3QS, United Kingdom Email: [EMAIL PROTECTED] Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Formatting phone numbers?
On Thu, Apr 15, 2004 at 06:11:57PM -0400, John W. Holmes wrote: Rob Ellis wrote: On Thu, Apr 15, 2004 at 04:31:09PM -0500, BOOT wrote: I'm looking for a way to take a 7 digit number and put it into xxx- format. So basically the logic is to count 3 characters into $number and insert a - there. substr_replace($string, '-', 3, 0); Won't that replace the number, though, not insert the dash? No, it does the right thing. The last 0 is the number of characters to replace. - Rob -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Formatting phone numbers?
On Thu, Apr 15, 2004 at 04:31:09PM -0500, BOOT wrote: Thanks for any help, even if you just suggest built in functions to look at. I'm looking for a way to take a 7 digit number and put it into xxx- format. So basically the logic is to count 3 characters into $number and insert a - there. substr_replace($string, '-', 3, 0); - Rob -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Sorting array of objects
On Mon, Apr 05, 2004 at 05:03:17AM +0200, Richard Harb wrote: Hi there, Supposed I have an array of objects, like: $this[$i]-property1 $this[$i]-property2 is there any 'cheap' way to sort the array according to the values of property1? Try array_multisort() - // create a temporary array of just the property // you want to sort on... foreach ($array_of_objects as $k = $o) $temp[$k] = $o-property1; // sort array_multisort($temp, $array_of_objects); - Rob -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] 'Content-Type: text/plain; charset=utf-8'
On Thu, Apr 01, 2004 at 01:55:50PM +0300, nabil wrote: ?php header ('Content-Type: text/plain; charset=utf-8'); echo Hi there; ? I need to echo a plain txt on my page, without any HTML tags. my problem, when I run it on my apache, the save/open screen pops up. What's wrong please help this works fine for me, cutting and pasteing your example. - rob -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] PHP charset encoding
On Tue, Mar 30, 2004 at 11:23:03AM +0300, nabil wrote: Hi all, I have problem storing the submitted data in the database in the right encoding. I need to use UTF-8 I need to save the Arabic text in UTF-8 encoding. I have a problem with UTF-8 and windows-1256 conversion. I wish I can understand those things, coz encoding thing will take my hair off When submitting a data from an HTML page and inserting them in MySQL: what encoding they will be ? is it the page encoding? the field size like VARCHAR 15 won't fit the same in both encoding for the same text. !! Is the font any relation with encoding? phpmyadmin doesn't support UTF-8 so dumping your data using it will screw it up is it a way to convert it inside the database... Explain to me please , or if you can tell me where to find my answers (not google) if the page that the input form is on sets utf-8 as the content type, then most (?) browsers will send utf-8. you can use a meta tag like: meta http-equiv=Content-Type content=text/html; charset=utf-8 utf-8 characters are 8 bit clean, so they can be stored and retrieved in mysql 3.x ok, but proper utf-8 sorting etc. doesn't work. for many purposes though it seems to be ok. single characters in utf-8 encoding do look like multiple characters as far as 8 bit text handling programs are concerned (so yes, 15 characters in iso-8859-1 or ascii isn't the same length as 15 characters in utf-8). fonts that support utf-8 are required for properly displaying utf-8, and you have to specify utf-8 on the display page (either via a content-type header or with content-type meta tag as above) to get the browser to use the right fonts. - rob -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] how does array_multisort work?(!??)
On Thu, Mar 18, 2004 at 02:30:56PM -0500, David T-G wrote: Hi, all -- I have an array like $a = array ( 'key' = array ( 'title' = Topic Title, 'content' = Topic Content, ), ... ) ; and I'd like to sort the whole thing not on the keys but on the titles. It sounds like array_multisort should do exactly what I want, but I can't seem to get it to work. For a given source array kind of like $a bookmark How To Bookmark This is all about bookmarking. add Adding Pictures Let's talk about pictures. zebra Caught You Here This is about zebras but it has a surprise title. then I want $a add Adding Pictures Let's talk about pictures. zebra Caught You Here This is about zebras but it has a surprise title. bookmark How To Bookmark This is all about bookmarking. after sorting. Will array_multisort sort on an arbitrary key of a multidimensional array, or does it just sort multidimensional arrays but only in key index priority? Barring that, I imagine I'd have to either ensure that the array is constructed in the proper order or make an index of titles = keys so that I could sort that and then pull the key out to display the list. Bleah. Once again it would probably be easier to just lay out a DB schema! it looks like array_multisort() will sort one array based on a sort of another... so you could do something like // create an array of titles that can be sorted foreach ($a as $k = $v) $titles[$k] = $v['title']; // sort $titles, then sort $a like $titles array_multisort($titles, $a); - rob -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Small Problem - could you help me, please?
On Thu, Mar 18, 2004 at 12:46:52AM +0200, Labunski wrote: I have a small problem with the script - could you hepl me, please? I made small kicking system for those who tried to guess the Login information. The script below is just a part of the code, but it's not working properly. I tried to solve this problem in many ways, but I couldn't. This script should check if the visitor's IP is listed in log.txt file and if it is, the warning appears. But the problem is that the script checks only first line in the log.txt file, not all the lines. P.S. log.txt file has many IP addresses listed in one column. $ip = getenv (REMOTE_ADDR); function kick() { $file_ip = file(log.txt); foreach($file_ip as $value_ip ) { $kick .= $value_ip; } return $kick; } if ($ip == kick()){ echo Sorry, the access is denied.; exit; } I think you want something like: if (in_array($_SERVER['REMOTE_ADDRESS'], file('log.txt'))) die(Sorry, the access is denied.); - Rob -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Small Problem - could you help me, please?
On Wed, Mar 17, 2004 at 06:11:31PM -0500, Jake McHenry wrote: I think you want something like: if (in_array($_SERVER['REMOTE_ADDRESS'], file('log.txt'))) die(Sorry, the access is denied.); - Rob yes, that'll work too.. just more compact than what I posted :-p oops, should actually be 'REMOTE_ADDR' not 'REMOTE_ADDRESS'. :-) - rob -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regex help - PLease
On Tue, Mar 16, 2004 at 02:39:27PM +0200, Brent Clark wrote: Hi there im in desperate need of help for a reg expression to ONLY allow 8 NUMBERS to start with 100 and may not have any other kind of letters or characters. Only numbers for example 10064893 if (preg_match('/^100\d{5}/', $test)) print ok\n; - rob -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] deleting array elements
On Tue, Mar 09, 2004 at 05:22:37PM -, Benjamin Jeeves wrote: Hi All I have two array one with a list of items in it. Then a second array with a list of items in it what I want to be able to do is compare array1 to array2 and if a match is found in both arrays delete that match from array1 and then so now? Any help would be good. so array1 = (1,2,3,4,5) array2 = (1,3,5) then print array1 and the output be 2,4 $array1 = array_diff($array1, $array2); - rob -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php