php-general Digest 29 Oct 2005 09:47:24 -0000 Issue 3764
php-general Digest 29 Oct 2005 09:47:24 - Issue 3764 Topics (messages 224791 through 224800): Re: calling static method within class 224791 by: Carlo Razzeto 224798 by: Tom Rogers Re: foreach / unset 224792 by: Richard Lynch UTF-8 to ISO-8859-1 224793 by: Richard Lynch Re: OCI8 224794 by: Richard Lynch Re: Decompressing a string with zlib problems 224795 by: Graham Anderson Re: DataCodecCompress and zLib problem (solved) 224796 by: Graham Anderson 224797 by: Graham Anderson Referencing Containing (Non-Parent) Object? 224799 by: Morgan Doocy Re: Inserting NULL Integer Values 224800 by: Bogdan Ribic Administrivia: To subscribe to the digest, e-mail: [EMAIL PROTECTED] To unsubscribe from the digest, e-mail: [EMAIL PROTECTED] To post to the list, e-mail: php-general@lists.php.net -- ---BeginMessage--- If you're expecting the statement Echo Foo::getUs(); To echo me me it will not because you never returned $us from the getUs() method. I'm assuming also that xxx is a variable to mean some class library correct? Carlo Razzeto Programmer Mortgage Information Services Phone: (216) 514-1025 ex. 1212 -Original Message- From: blackwater dev [mailto:[EMAIL PROTECTED] Sent: Friday, October 28, 2005 1:13 PM To: php-general@lists.php.net Subject: [PHP] calling static method within class I have a class that won't be instantiated...so basically just a bunch of static methods. How do I call one of the class' static methods from within another method? Can't use this, and self doesn't work...this is php4. Thanks. Class Foo{ function getMe(){ return me; } function getUs(){ $us=xxx::getMe(); $us.= me; } } echo Foo::getUs(); -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php Disclaimer: This message (including any attachments) contains confidential information intended for a specific individual and purpose, and is protected by law. If you are not the intended recipient, you should delete this message. Any disclosure, copying, or distribution of this message, or the taking of any action based on it, is strictly prohibited. ---End Message--- ---BeginMessage--- Hi, Saturday, October 29, 2005, 5:42:15 AM, you wrote: CR If you're expecting the statement CR Echo Foo::getUs(); CR To echo me me it will not because you never returned $us from the CR getUs() method. I'm assuming also that xxx is a variable to mean some CR class library correct? CR Carlo Razzeto CR Programmer CR Mortgage Information Services CR Phone: (216) 514-1025 ex. 1212 CR -Original Message- CR From: blackwater dev [mailto:[EMAIL PROTECTED] CR Sent: Friday, October 28, 2005 1:13 PM CR To: php-general@lists.php.net CR Subject: [PHP] calling static method within class CR I have a class that won't be instantiated...so basically just a bunch CR of static methods. CR How do I call one of the class' static methods from within another CR method? Can't use this, and self doesn't work...this is php4. CR Thanks. CR Class Foo{ CRfunction getMe(){ CR return me; CR } CR function getUs(){ CR$us=xxx::getMe(); CR$us.= me; CR } CR } CR echo Foo::getUs(); Just use the class name Foo::getMe() Class Foo{ function getMe(){ return me; } function getUs(){ $us=Foo::getMe(); $us.= me; } } -- regards, Tom ---End Message--- ---BeginMessage--- On Fri, October 28, 2005 8:47 am, Jochem Maas wrote: John Nichel wrote: Richard Lynch wrote: Somewhere in the manual (damned if I can find it now) it says (or used to say) that you can or can't safely do this: while (list($k, $v) = each($array)){ if (...) unset($array[$k]); } I don't even remember if it's safe or not, but I swear I saw it not that long ago... Anyway, can you do *this* safely as a DOCUMENTED FEATURE: foreach($array as $k = $v){ if (...) unset($array[$k]); } I'm sure I could test it and maybe find out if it works but is it documented behaviour I can rely on? I'm sure not finding this in the manual now that I go looking for it, though I know I saw it there before. I would think it would be just fine *EXCEPT* that the internal pointer of the original array is being incremented in parallel with the copied array key/values, so it ends up at the end after the foreach So, unless it's a DOCUMENTED FEATURE rather than an implementation detail, I don't really want to rely on unset($original[$k]) working because who knows what might change in PHP6 with the internal pointer and unsetting the current entry it points to, or, rather, the entry it previously pointed to. I know for sure that while/list/each and an integer-indexed array and (unset($array[$k + 1])) is a big mistake :-) I would *think* (just my opinion without much thought on a Friday morning)
[PHP] Referencing Containing (Non-Parent) Object?
I'm trying to figure out if PHP has the facility to reference containing, non-parent objects. I have three classes, embedded hierarchically, but which are NOT extended classes of their containing objects. I'd like to be able to reference variables in the higher-level objects from the lower-level objects. To illustrate: ?php class Country { var $name; var $states; function Country($name = '', $states = array()) { $this-name = $name; $this-states = $states; } } class State { var $name; var $cities; function State($name = '', $cities = array()) { $this-name = $name; $this-cities = $cities; } } class City { var $name; fuction City($name = '') { $this-name = ''; } function name_with_state() { return $this-name, . /* parent node reference */-name; } } $countries = array ( 'USA' = new Country('United States', array ( 'WA' = new State('Washington', array ( 'SEA' = new City('Seattle'), 'GEG' = new City('Spokane') ) ), 'CA' = new State('California', array ( 'LAX' = new City('Los Angeles'), 'SFO' = new City('San Francisco') ) ) ) ) ); echo $countries['USA']-states['WA']-cities['SEA']-name_with_state(); // Output: 'Seattle, Washington' ? Obviously, just extending Country doesn't make sense: a State isn't a type of Country, and a City isn't a type of State -- but States are part of Countries, and Cities are part of States. Plus, if City was just an extended State (which in turn is an extended Country), it would contain both a $states and a $cities variable. Which doesn't make sense. What, if anything, do I replace /* parent node reference */ with in City::name_with_state()? Thanks, Morgan -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Inserting NULL Integer Values
Oliver Grätz wrote: Shaun schrieb: $qid = mysql_query('INSERT INTO MYTABLE ( column1, column2, ) VALUES ( '.$value1.', '.$value2.' )'); A bit off-topic but important: Always make sure that you check the contents of $value1 and $value2 before putting them into the query! With $value1 = 'xyz,xyz); DELETE FROM MYTABLE;'; you might get surprising results! This is called SQL injection and it's important to escape all the values before putting them into the statement. Did you try that? This doesn't work on my machine: mysql_query(DELETE FROM mytable; DELETE FROM mytable;); ie, mysql extension won't let me do more than one statement at a time. -- Open source PHP code generator for DB operations http://sourceforge.net/projects/bfrcg/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Images outside webroot + session = safe?
Hi there, I am storing images outside the webroot to keep them from being accessible for unauthorized users to see. Then I use a script to show the images, like this: img src=show.php Now, as there is no information on the images stored in a database yet (they have just been uploaded via ftp), I need to find a way of passing the information as to which image is to be displayed. I am currently trying out this way: 1. I read the filenames for all images in the upload directory into an array. 2. I store that array in a session variable. $_SESSION['images'] = $this-image_array; 3. I call show.php passing an array key: img src=show.php?id=xy 4. In show.php I start the session, get the image information from the session array, check if the mime type is okay and then display the image. Of course I still need to add user authorization... Any opinions on how safe this method seems or how it could be made safer/more efficient? Do you think this method could be exploited to compromise the server in any way? Here the listing for show.php ?php session_start(); $file = $_SESSION['images'][$_GET['id']]; if(is_file($file['path'].$file['file'])) { //determine mime type and imagetype $tmp = getimagesize($file['path'].$file['file']); $file['mime'] = $tmp['mime']; //if file is of valid type - output to browser if(in_array($file['mime'], $_SESSION['conf']['images']['allowedtypes'])) { header(Content-Type: .$file['mime']); header(Content-Disposition: filename=.$file['name']); readfile($file['path'].$file['file']); } } ? Any comments are appreciated. jt -- Lust, ein paar Euro nebenbei zu verdienen? Ohne Kosten, ohne Risiko! Satte Provisionen für GMX Partner: http://www.gmx.net/de/go/partner -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: foreach / unset
Richard Lynch wrote: Anyway, can you do *this* safely as a DOCUMENTED FEATURE: foreach($array as $k = $v){ if (...) unset($array[$k]); } Well, somewhere in the said manual it is written that foreach operates on a *copy* of the array, so you should be safe unsetting values in the original array while iterating through the copy. -- Open source PHP code generator for DB operations http://sourceforge.net/projects/bfrcg/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Referencing Containing (Non-Parent) Object?
Morgan Doocy wrote: I'm trying to figure out if PHP has the facility to reference containing, non-parent objects. I have three classes, embedded hierarchically, but which are NOT extended classes of their containing objects. I'd like to be able to reference variables in the higher-level objects from the lower-level objects. To illustrate: ?php class Country { var $name; var $states; function Country($name = '', $states = array()) { $this-name = $name; $this-states = $states; } } class State { var $name; var $cities; function State($name = '', $cities = array()) { $this-name = $name; $this-cities = $cities; } } class City { var $name; fuction City($name = '') { $this-name = ''; } function name_with_state() { return $this-name, . /* parent node reference */-name; } } $countries = array ( 'USA' = new Country('United States', array ( 'WA' = new State('Washington', array ( 'SEA' = new City('Seattle'), 'GEG' = new City('Spokane') ) ), 'CA' = new State('California', array ( 'LAX' = new City('Los Angeles'), 'SFO' = new City('San Francisco') ) ) ) ) ); echo $countries['USA']-states['WA']-cities['SEA']-name_with_state(); // Output: 'Seattle, Washington' ? Obviously, just extending Country doesn't make sense: a State isn't a type of Country, and a City isn't a type of State -- but States are part of Countries, and Cities are part of States. Plus, if City was just an extended State (which in turn is an extended Country), it would contain both a $states and a $cities variable. Which doesn't make sense. What, if anything, do I replace /* parent node reference */ with in City::name_with_state()? Thanks, Morgan I suspect you want the functionality of Java embedded clases, where embedded class can access fields of its container. If that's what you mean, then sorry, PHP doesn't have that. You would have to keep a reference ro parent object as a variable in child object, ie your City should have a var $_state variable, etc. -- Open source PHP code generator for DB operations http://sourceforge.net/projects/bfrcg/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Substr by words
Hi, I need to extract 50 words more or less from a description field. How can i do that?. Substr, cuts the words. Is there any other way to that, without using and array? I mean and implemented function in PHP 4.x I´ve been googling around, but wordwrap, and substr is driving me mad... Thanks in advance Best Regards -- dpc
[PHP] [DONE] Substr by words
Finally i found it (Google is god, you only have to ask the right question) function trim_text($text, $count){ $text = str_replace( , , $text); $string = explode( , $text); for ( $wordCounter = 0; $wordCounter = $count;wordCounter++ ){ $trimed .= $string[$wordCounter]; if ( $wordCounter $count ){ $trimed .= ; } else { $trimed .= ...; } } $trimed = trim($trimed); return $trimed; } Usage $string = one two three four; echo trim_text($string, 3); -- Forwarded message -- From: Danny [EMAIL PROTECTED] Date: Oct 29, 2005 1:36 PM Subject: Substr by words To: php-general@lists.php.net Hi, I need to extract 50 words more or less from a description field. How can i do that?. Substr, cuts the words. Is there any other way to that, without using and array? I mean and implemented function in PHP 4.x I´ve been googling around, but wordwrap, and substr is driving me mad... Thanks in advance Best Regards -- dpc -- dpc
[PHP] Sessions and register_long_arrays
Strange behaviour that's taken me ages to track down. I have the situation where I can create a session, but any changes to it are not saved. session_write_close() didn't help. Eventually I tracked it down: if you have register_long_arrays disabled (as is the default in PHP5), this can happen. Enabling it fixed the problem. A very simple test case didn't show this problem, so I guess something in my sessions has a dependency on HTTP_GET_VARS or similar, though these old-style vars do not appear anywhere in my code... Some of the libraries I'm using may use them (for example Smarty, though I have the request_use_auto_globals option enabled for that which should stop it using them), but nothing to do with them is stored in the session. If I look at a session file, it's all just scalars and arrays, no complex types at all, but changing an item in $_SESSION simply does not get saved back to the session file if register_long_arrays is enabled. Anyone else seen this? Any idea why it might be happening? Marcus -- Marcus Bointon Synchromedia Limited: Putting you in the picture [EMAIL PROTECTED] | http://www.synchromedia.co.uk -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Sessions and register_long_arrays
On 29 Oct 2005, at 14:48, Marcus Bointon wrote: changing an item in $_SESSION simply does not get saved back to the session file if register_long_arrays is enabled. I meant disabled. I've also tried using it with the mm session save handler and I get the same symptoms. I also get identical results on OS X and Linux. FYI, I want to disable this setting for increased performance, especially as I'm not using these old arrays anyway. Marcus -- Marcus Bointon Synchromedia Limited: Putting you in the picture [EMAIL PROTECTED] | http://www.synchromedia.co.uk -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] [DONE] Substr by words
Good job! However, let me give a few suggestions to optimize the code function trim_text($text, $count) { $text = str_replace( , , $text); /* * This is redundant; you are replacing all in $text with * maybe you meant * $text = trim($text); ? */ $string = explode( , $text); /* * For better programming practice, you should initialize $trimed * I believe if you turn on error_reporting for all in php.ini * PHP will display all warnings and errors. */ for ( $wordCounter = 0; $wordCounter = $count;wordCounter++ ) /* * Typo - you forgot the $ for wordCounter++ * * for ( $wordCounter = 0; $wordCounter = $count; $wordCounter++) */ { $trimed .= $string[$wordCounter]; if ( $wordCounter $count ) { $trimed .= ; } else { $trimed .= ...; } } $trimed = trim($trimed); return $trimed; } This is purely my suggestion... and I'm not saying it is better... but if I were you, I'd do it this way : function trim_text($text, $count) { $text = trim($text); $string = explode( , $text); $trimed=''; for ( $wordCounter = 0; $wordCounter = $count; $wordCounter++ ) { $trimed .= $string[$wordCounter].' '; } $trimed = trim($trimed); if (count($string)$count) $trimed.='...'; return $trimed; } The only difference(not that you'd notice in a lightweight routine like this) is that, I don't have that if-else block inside the loop. Danny wrote: Finally i found it (Google is god, you only have to ask the right question) function trim_text($text, $count){ $text = str_replace( , , $text); $string = explode( , $text); for ( $wordCounter = 0; $wordCounter = $count;wordCounter++ ){ $trimed .= $string[$wordCounter]; if ( $wordCounter $count ){ $trimed .= ; } else { $trimed .= ...; } } $trimed = trim($trimed); return $trimed; } Usage $string = one two three four; echo trim_text($string, 3); -- Forwarded message -- From: Danny [EMAIL PROTECTED] Date: Oct 29, 2005 1:36 PM Subject: Substr by words To: php-general@lists.php.net Hi, I need to extract 50 words more or less from a description field. How can i do that?. Substr, cuts the words. Is there any other way to that, without using and array? I mean and implemented function in PHP 4.x I´ve been googling around, but wordwrap, and substr is driving me mad... Thanks in advance Best Regards -- dpc -- dpc -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] is there a number translation function?
Hi, Does anyone know of a function for translating a decimal number into an English number. In other words, if you pass it 1 it will return 'one', if you pass it 127 it will return 'one hundred twenty seven', and etc. Thanks, Linda -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] [DONE] Substr by words
Good start guys. That's usually how I start down the path in solving a new problem. Invariably down the road I find a more refined way of doing what I did the hard way. The hard way is good though, you learn stuff along the way. Let's see how tight we can make this though: ?php $string = This is a test string of some sort. ; $truncated = truncstr($string); echo $truncated; function truncstr($tmpstr) { $maxwords = 4; $tmpstr = trim($tmpstr); $wordarr = explode( , $tmpstr); if (count($wordarr) $maxwords) { $wordarr = array_slice($wordarr, 0, $maxwords); $tmpstr = implode( , $wordarr) . ...; } else { $tmpstr = implode( , $wordarr); } return $tmpstr; } ? Or if you want really obfuscated code... ?PHP $string = This is a test string of some sort. ; $truncated = truncstr($string); echo $truncated; function truncstr($tmpstr) { return implode( , array_slice(explode( , trim($tmpstr)), 0, 4)) . ...; } ? -TG = = = Original message = = = Good job! However, let me give a few suggestions to optimize the code function trim_text($text, $count) ~$text = str_replace( , , $text); ~/* ~ * This is redundant; you are replacing all in $text with ~ * maybe you meant ~ * $text = trim($text); ? ~ */ ~$string = explode( , $text); ~ ~/* ~ * For better programming practice, you should initialize $trimed ~ * I believe if you turn on error_reporting for all in php.ini ~ * PHP will display all warnings and errors. ~ */ ~for ( $wordCounter = 0; $wordCounter = $count;wordCounter++ ) ~/* ~ * Typo - you forgot the $ for wordCounter++ ~ * ~ * for ( $wordCounter = 0; $wordCounter = $count; $wordCounter++) ~ */ ~ ~~$trimed .= $string[$wordCounter]; ~~if ( $wordCounter $count ) ~~ ~~~$trimed .= ; ~~ ~~else ~~ ~~~$trimed .= ...; ~~ ~ ~$trimed = trim($trimed); ~return $trimed; This is purely my suggestion... and I'm not saying it is better... but if I were you, I'd do it this way : function trim_text($text, $count) ~$text = trim($text); ~$string = explode( , $text); ~$trimed=''; ~for ( $wordCounter = 0; $wordCounter = $count; $wordCounter++ ) ~ ~~$trimed .= $string[$wordCounter].' '; ~ ~$trimed = trim($trimed); ~if (count($string)$count) ~~$trimed.='...'; ~return $trimed; The only difference(not that you'd notice in a lightweight routine like this) is that, I don't have that if-else block inside the loop. Danny wrote: Finally i found it (Google is god, you only have to ask the right question) function trim_text($text, $count) $text = str_replace( , , $text); $string = explode( , $text); for ( $wordCounter = 0; $wordCounter = $count;wordCounter++ ) $trimed .= $string[$wordCounter]; if ( $wordCounter $count ) $trimed .= ; else $trimed .= ...; $trimed = trim($trimed); return $trimed; Usage $string = one two three four; echo trim_text($string, 3); -- Forwarded message -- From: Danny [EMAIL PROTECTED] Date: Oct 29, 2005 1:36 PM Subject: Substr by words To: php-general@lists.php.net Hi, I need to extract 50 words more or less from a description field. How can i do that?. Substr, cuts the words. Is there any other way to that, without using and array? I mean and implemented function in PHP 4.x I~ve been googling around, but wordwrap, and substr is driving me mad... Thanks in advance Best Regards -- dpc -- dpc ___ Sent by ePrompter, the premier email notification software. Free download at http://www.ePrompter.com. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: curious (and frustrating) php/apache behavior
We're having a problem getting more than one imbedded PHP script to execute in our Apache-served pages. We're using Apache 2.0.40 server-side includes. We wish to include multiple PHP scripts into our pages but are only succeeding in having the first included PHP script executed; the rest are ignored or misinterpreted as HTML... Thank you in advance for any help you provide. This works: $ cat php1.txt ?php echo this is the first php include; ? $ cat test.shtml htmlbody !--#INCLUDE virtual=/php1.txt-- brthis came from html /body/html Producing the expected result from the browser (http://myhost/test.shtml): this is the first php include this is html However, this does not produce the expected three line result: $ cat php1.txt ?php echo this is the first php include; ? $ cat php2.txt ?php echo this is the second php include; ? $ cat test.shtml htmlbody !--#INCLUDE virtual=/php1.txt-- br this came from html br !--#INCLUDE virtual=/php2.txt-- /body/html Producing from the browser (http://myhost/test.shtml): this is the first php include this is html This DOES work: $cat test.php htmlbody ?php echo this is from the first php block; ? br this is from html br ?php echo this is from the second php block; ? /body/html Producing from the browser (http://myhost/test.php): this is from the first php block this is from html this is from the second php block Don Brown Co-Founder, Utah Skies Ski champagne powder by day, surf diamond-studded velvet by night... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Retrieve Data Formated from Text field
Hi Gurus, I´ve got a problem with the result displaying a TEXT type field The data is inserted into db in this way: Hello, This is a sample of first line. This is another paragraph, blah blah blah Bye But the result is in one paragraph: Hello, This is a sample of first line. This is another paragraph, blah blah blah Bye If I use HTML PRE label, I´ve got the result correct, but cannot use styles Any Suggestion? thx . -- dpc
[PHP] curious (and frustrating) php/apache behavior
We're having a problem getting more than one imbedded PHP script to execute in our Apache-served pages. We're using Apache 2.0.40 server-side includes. We wish to include multiple PHP scripts into our pages but are only succeeding in having the first included PHP script executed; the rest are ignored or misinterpreted as HTML... Thank you in advance for any help you provide. This works: $ cat php1.txt ?php echo this is the first php include; ? $ cat test.shtml htmlbody !--#INCLUDE virtual=/php1.txt-- brthis came from html /body/html Producing the expected result from the browser (http://myhost/test.shtml): this is the first php include this is html However, this does not produce the expected three line result: $ cat php1.txt ?php echo this is the first php include; ? $ cat php2.txt ?php echo this is the second php include; ? $ cat test.shtml htmlbody !--#INCLUDE virtual=/php1.txt-- br this came from html br !--#INCLUDE virtual=/php2.txt-- /body/html Producing from the browser (http://myhost/test.shtml): this is the first php include this is html This DOES work: $cat test.php htmlbody ?php echo this is from the first php block; ? br this is from html br ?php echo this is from the second php block; ? /body/html Producing from the browser (http://myhost/test.shtml): this is from the first php block this is from html this is from the second php block Don Brown Co-Founder, Utah Skies Ski champagne powder by day, surf diamond-studded velvet by night...
Re: [PHP] curious (and frustrating) php/apache behavior
We wish to include multiple PHP scripts into our pages but are only succeeding in having the first included PHP script executed; the rest are ignored or misinterpreted as HTML... I don't know if this is part of your problem, but when you do an include, it throws you out of php. So, if there is any php code in the include, you can't rely on the include being inside a php block. You must put a php block inside the include as well. This is true no matter how you name the include file (.php, .inc, .htm). Linda H -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Retrieve Data Formated from Text field
The data is inserted into db in this way: Hello, This is a sample of first line. This is another paragraph, blah blah blah Bye But the result is in one paragraph: Hello, This is a sample of first line. This is another paragraph, blah blah blah Bye It sounds as if your database record includes a carriage return or new line character (\n), which is not recognized in html (except using the pre tag. You need to replace the new line with an html p tag. You might want to store each paragraph in a different database record (make a table for the text. It will include a key that connects it to the main record, the text, and a sequence number that tells you the order in which the paragraphs should be displayed). Linda H -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] is there a number translation function?
I couldn't find one out there Linda (and I'm sure I've seen them before) so since I'm stuck at work on a Saturday I thought I'd take a break and see if I could hack this out myself. Probably a more elegant way to do this, but here's what I came up with. Feel free to use and modify it at will. It should be infinitely expandable by adding to the $places array. (well, 'infinite' in regards to your system's ability to handle long strings, no long ints or floats are used so that shouldn't limit it). Let me know if you have any questions or if anyone can improve this (always up for learning new tricks :) Good luck Linda! -TG ?PHP $numerals = array(1 = one, 2 = two, 3 = three, 4 = four, 5 = five, 6 = six, 7 = seven, 8 = eight, 9 = nine, 10 = ten, 11 = eleven, 12 = twelve, 13 = thirteen, 14 = fourteen, 15 = fifteen, 16 = sixteen, 17 = seventeen, 18 = eighteen, 19 = nineteen, 20 = twenty, 30 = thirty, 40 = fourty, 50 = fifty, 60 = sixty, 70 = seventy, 80 = eighty, 90 = ninety ); $places = array(1 = hundred, 2 = thousand, 3 = million, 4 = billion, 5 = trillion, 6 = quadrillion ); $testnumber = 1,205,513; $number = str_replace(,, , trim($testnumber)); $rev_number = strrev($number); $rev_numberarr = explode(|||, wordwrap($rev_number, 3, |||, 1)); $x = 1; $outarr = array(); foreach ($rev_numberarr as $order = $rev_num_block) { $tmpoutput = ; $num_block = sprintf(%03s, strrev($rev_num_block)); list($pos_one, $pos_two, $pos_three) = explode(|||, wordwrap($num_block, 1, |||, 1)); if (intval($pos_one) 0) $tmpoutput = $numerals[$pos_one] . hundred . $tmpoutput . ; if (intval($pos_two . $pos_three) = 20 OR (intval($pos_two) 0 AND intval($pos_three) == 0)) { $tmpoutput .= $numerals[intval($pos_two . $pos_three)]; } elseif (intval($pos_two) == 0) { $tmpoutput .= $numerals[$pos_three] . $tmpoutput; } elseif (intval($pos_two) 0 AND intval($pos_three) 0) { $tmpoutput .= $numerals[$pos_two . $pos_three] . $tmpoutput; } if ($x 1) $tmpoutput .= . $places[$x]; array_unshift($outarr, $tmpoutput); //$output = $tmpoutput . . $output;# Can just use this instead of array_unshift if you don't need to put comma dividers $x++; } $output = implode(, , $outarr); // If you don't use an array, remove this line too echo $testnumber . br\n; echo becomesbr\n; echo \ . ucwords($output) . \; ? = = = Original message = = = Hi, Does anyone know of a function for translating a decimal number into an English number. In other words, if you pass it 1 it will return 'one', if you pass it 127 it will return 'one hundred twenty seven', and etc. Thanks, Linda ___ Sent by ePrompter, the premier email notification software. Free download at http://www.ePrompter.com. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] is there a number translation function?
On Sat, 2005-10-29 at 12:13 -0700, Linda H wrote: Does anyone know of a function for translating a decimal number into an English number. In other words, if you pass it 1 it will return 'one', if you pass it 127 it will return 'one hundred twenty seven', and etc. http://pear.php.net/package-info.php?package=Numbers_Words -- Greg Donald Zend Certified Engineer MySQL Core Certification http://destiney.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Substr by words
On Sat, October 29, 2005 6:36 am, Danny wrote: I need to extract 50 words more or less from a description field. How can i do that?. Substr, cuts the words. Is there any other way to that, without using and array? I mean and implemented function in PHP 4.x I´ve been googling around, but wordwrap, and substr is driving me mad... Keep in mind that the pipe between your database and PHP is a rather small narrow expensive opening. Sucking down your ENTIRE description field to throw away all but 50 characters may not be the best use of limited resources. [This is all MOOT if you have no dreams of your site being big some day.] You therefore may want to consider something like: select substring(description, 1, instr(description, ' ', 50)) as description_50, substring(description, instr(description, ' ', 50), 1) as more from ... $description_50 will be 50 chars, more or less $more will tell you if there was more or not You will only be getting ~50 characters squeezed through that narrow expensive db - PHP pipeline. I believe that in MOST PHP/database applications this is going to be a better performing solution, and it's somewhat cleaner aesthetically than shoveling a bunch of data around that you're going to discard anyway. * instr may or may not be the right function in your database. I always forget the name of this one and have to look it up. -- Like Music? http://l-i-e.com/artists.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: [DONE] Substr by words
Did you actually test that? Even better version of your function, code $string = one two three four; echo substr($string, -4); echo '...'; /code James Danny wrote: Finally i found it (Google is god, you only have to ask the right question) function trim_text($text, $count){ $text = str_replace( , , $text); $string = explode( , $text); for ( $wordCounter = 0; $wordCounter = $count;wordCounter++ ){ $trimed .= $string[$wordCounter]; if ( $wordCounter $count ){ $trimed .= ; } else { $trimed .= ...; } } $trimed = trim($trimed); return $trimed; } Usage $string = one two three four; echo trim_text($string, 3); -- Forwarded message -- From: Danny [EMAIL PROTECTED] Date: Oct 29, 2005 1:36 PM Subject: Substr by words To: php-general@lists.php.net Hi, I need to extract 50 words more or less from a description field. How can i do that?. Substr, cuts the words. Is there any other way to that, without using and array? I mean and implemented function in PHP 4.x I´ve been googling around, but wordwrap, and substr is driving me mad... Thanks in advance Best Regards -- dpc -- dpc -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] [DONE] Substr by words
On Sat, October 29, 2005 10:51 am, Minuk Choi wrote: function trim_text($text, $count) { $text = str_replace( , , $text); /* * This is redundant; you are replacing all in $text with * maybe you meant * $text = trim($text); ? */ It was probably replacing *TWO* spaces with one. If so, it should really be in a while loop, because there could be 3 or more spaces in a row, and if the goal is only single-spaced words... //Replace 2 spaces with 1 until all words are single-spaced while (strstr($text, ' ')) $text = str_replace(' ', ' ', $text); PS My post was doing 50 LETTERS, not 50 words. So change 50 to 250 and call it done. :-) Unless the word-count is really that critical... -- Like Music? http://l-i-e.com/artists.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Inserting NULL Integer Values
On Sat, October 29, 2005 4:45 am, Bogdan Ribic wrote: $value1 = 'xyz,xyz); DELETE FROM MYTABLE;'; you might get surprising results! This is called SQL injection and it's important to escape all the values before putting them into the statement. Did you try that? This doesn't work on my machine: mysql_query(DELETE FROM mytable; DELETE FROM mytable;); ie, mysql extension won't let me do more than one statement at a time. PHP MySQL has not allowed multiple statements per query for awhile, I think. I also think it's possible to change that, or that it might change in the future. Regardless of all that, the general principle remains sound. Even if the one specific example does not work, that doesn't mean that there aren't a few billion that WILL work to compromise your site. http://phpsec.org -- Like Music? http://l-i-e.com/artists.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: curious (and frustrating) php/apache behavior
Hi, why don't you use normal php include (require) functions? Why do you want to mix apache server side includes with php code? There is nothing php include can't do.. Just change all !--#INCLUDE virtual=-- to ? include(''); ? Petr Don Brown wrote: We're having a problem getting more than one imbedded PHP script to execute in our Apache-served pages. We're using Apache 2.0.40 server-side includes. We wish to include multiple PHP scripts into our pages but are only succeeding in having the first included PHP script executed; the rest are ignored or misinterpreted as HTML... Thank you in advance for any help you provide. This works: $ cat php1.txt ?php echo this is the first php include; ? $ cat test.shtml htmlbody !--#INCLUDE virtual=/php1.txt-- brthis came from html /body/html Producing the expected result from the browser (http://myhost/test.shtml): this is the first php include this is html However, this does not produce the expected three line result: $ cat php1.txt ?php echo this is the first php include; ? $ cat php2.txt ?php echo this is the second php include; ? $ cat test.shtml htmlbody !--#INCLUDE virtual=/php1.txt-- br this came from html br !--#INCLUDE virtual=/php2.txt-- /body/html Producing from the browser (http://myhost/test.shtml): this is the first php include this is html This DOES work: $cat test.php htmlbody ?php echo this is from the first php block; ? br this is from html br ?php echo this is from the second php block; ? /body/html Producing from the browser (http://myhost/test.shtml): this is from the first php block this is from html this is from the second php block Don Brown Co-Founder, Utah Skies Ski champagne powder by day, surf diamond-studded velvet by night... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Retrieve Data Formated from Text field
http://php.net/nl2br On Sat, October 29, 2005 12:07 pm, Danny wrote: Hi Gurus, I´ve got a problem with the result displaying a TEXT type field The data is inserted into db in this way: Hello, This is a sample of first line. This is another paragraph, blah blah blah Bye But the result is in one paragraph: Hello, This is a sample of first line. This is another paragraph, blah blah blah Bye If I use HTML PRE label, I´ve got the result correct, but cannot use styles Any Suggestion? thx . -- dpc -- Like Music? http://l-i-e.com/artists.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] curious (and frustrating) php/apache behavior
On Sat, October 29, 2005 12:01 pm, Don Brown wrote: We're having a problem getting more than one imbedded PHP script to execute in our Apache-served pages. We're using Apache 2.0.40 server-side includes. We wish to include multiple PHP scripts into our pages but are only succeeding in having the first included PHP script executed; the rest are ignored or misinterpreted as HTML... Thank you in advance for any help you provide. This works: $ cat php1.txt ?php echo this is the first php include; ? $ cat test.shtml htmlbody !--#INCLUDE virtual=/php1.txt-- You are using Server Side Include (SSI) here. SSI is to PHP as a GoCart is to a Ferrarri :-) *IF* your original page of HTML can be parsed as PHP, you'll have a lot less frustration and a lot more flexibility using: ?php include 'php1.txt'? instead. But let's assume that you are: A) Stuck with the HTML being HTML. B) Don't need variables/data from php1.txt to transfer to php2.txt later in the script C) Are willing to accept the increased HTTP performance hit of !-- #INCLUDE virtual... which, I *THINK* will chew up another HTTP connection If you're okay with all of that, you probably just need to change php1.txt to php1.php so that when it is requested, Apache knows that it is PHP code, and not plain text. *.txt - Apache mime-type makes Apache think it's plain/text *.php - Apache mime-type makes Apache think it's PHP These are just the defaults. You can configure Apache with ForceType (and friends) to make all your .txt files pass through PHP, or all your .htm or .html files pass through PHP. Whether you really WANT to pass *ALL* .txt files through PHP is pretty questionable. All .htm and .html files through PHP is quite common and has many benefits. PS I got no idea how you managed to get the FIRST php block to work and not the second... Unless one of them has .php as part of the filename -- Like Music? http://l-i-e.com/artists.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Type of form element
On Fri, October 28, 2005 10:00 am, Shaun wrote: I have some checkboxes on my page which correspond with boolean fields in my database - actually they are TINYINT's in which I store a 0 or 1 in for false and true values respectively. Is it possible to loop through all $_POST values to see if it is a checkbox? If so then for that element if it is equal to 'on' then change it to 1 otherwise change it to 0? No. The only things you get in POST are: name (string) value (string) PHP does provide the feature (some call it mis-feature) of array processing on name, so that: name[index] turns into $name['index'] If you want to identify your checkboxes as checkboxes, you will need some external, application-specific way to do so. You could: #1. Use Hungarian Notation (ugh!) in your checkbox field names, so you would know that any 'name' that starts with 'ckbx' was a checkbox. #2. Have an array of known checkbox fields in your PHP ?php $checkboxes = array('spam_me', 'read_terms', 'whatever'); ? And then you could compare each $_POST index with that array. Improving performance using $checkboxes with KEYS of the names instead of values is left as an exercise for the reader :-) You also need to be aware that HTTP does *NOT* transmit off checkboxes. If the user has 3 checkboxes, and selects only 1, you get *NOTHING* in $_POST about the other 2 checkboxes. The very lack of any POST data tells you the checkboxes are off So you will most likely be using isset($_POST['checkbox_name']) rather than testing for on -- Like Music? http://l-i-e.com/artists.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Type of form element
I usually place a hidden field with the same name as the checkbox field before the actual checkbox field. I store my 'false' value in there. If the checkbox is checked the hidden field is overridden. ?php error_reporting( E_ALL ); if( isset( $_POST[ 'submit' ] ) ) { echo 'pre'; print_r( $_POST ); echo '/pre'; } echo EOF form method='post' action='$_SERVER[PHP_SELF]' input type='hidden' value='0' name='blah' input type='checkbox' value='1' name='blah' Blah? input type='submit' name='submit' /form EOF; ? This probably works just fine in all browsers, but... I don't THINK the HTML and HTTP specification specificially require ordering of HTML/INPUT/POST elements to match up In fact, as I recall (and it's been YEARS since I read the damn thing) I believe they specifically said that the ordering was NOT to be relied upon... Though this may well have changed in HTTP/HTML 3.0, 4.0, XHTML, etc vesions of specifications. It also seems like rather needless bloat of HTML, to me, unless I'm missing something. $blah = isset($_POST['blah']); //update or process $blah If the processing of $blah is particularly expensive or heinous, and it's a pre-existing preference type of setting, I can see the savings, but given the overhead of checking every value going in/out to compare before update, it seems like the overhead outweighs the savings, and clutters up the code... -- Like Music? http://l-i-e.com/artists.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] printing from php
On Fri, October 28, 2005 6:17 am, Tom Cruickshank wrote: I've been reading up on printing out documents using PHP (using Printer functions calls in the php manual) I'm using a Linux and/or FreeBSD operating system to run my php code on (in apache). However, I am surfing these pages using a Windows XP machine. Has anyone ever tried having a print button (or link) in php that would make Whatever page is being displayed being printed with the above scenario? How might you of gone about it to make it work? (the Linux or FreeBSD box is not configured to have a printer on it, shared or local, does that make a difference? ) If you are trying to get the print-out on a printer tied to the server, PHP is maybe going to be involved. If you want it printed to a printer tied to the client (browser) PHP is long-gone and has nothing to do with it by the time you click on Print button. There are a bunch of JavaScript print buttons/links/scripts you can find with Google to help. If you need your HTML to be made printer-friendly BEFORE you print it, PHP and http://php.net/pdflib may help. Or is it http://php.net/libpdf? I can never remember. -- Like Music? http://l-i-e.com/artists.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] PHP5 class constants
On Thu, October 27, 2005 9:56 pm, Chris wrote: Though I suppose you could make an argument for using expressions that consist of only constant values. Actually... One could argue that so long as the programmer was willing to accept the consequences, there could be many legitimate circumstances in which one might WANT to utilize non-constant values for a const. Off the top of my head, there are: 1. A 'random' value which should be set for the course of the run/script 2. An environment 'variable' which comes from some external source. 3. A time() [and friends] value for profiling 4. Time computations such as (60*60*24). [Re #4] Not all Programmers have memorized and immediately recognize the value that 60*60*24 works out to, but they'd be hard pressed to not recognize those numbers as seconds/minutes/hours... In some languages, it is possible to use a pre-processor construct to have the interpreter/compiler compute a value in its first-pass, and to store that value as a constant in the program for actual execution. #. was what I recall from Lisp, about a decade ago. It was quite useful in many cases. I'm pretty sure C's macros and pre-processor macros and all that junk that gave me headaches was (partially) meant to accomplish the same thing. :-) I'm not sure PHP *needs* this feature, but I can certainly see that it would be useful to a lot more programmers than some stuff that is being worked on for PHP5+. :-) :-) :-) -- Like Music? http://l-i-e.com/artists.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Using PHP for accsess control, preventing access to static files
On Thu, October 27, 2005 11:05 am, Dan Trainor wrote: It's been suggested to use readfile() to accomplish this, by forwarding content from outside of the document root - but this just sounds odd. On top of being (what I think would be) incredibly slow, it just doesn't sound right. A) It's right. B) readfile is the same thing in PHP that Apache would do in Apache, basically: PHP::readfile == Apache::readfile So your overhead is a few function calls to the PHP Module, a load-up of your download.php script from the hard drive, and then a few function calls in PHP. Now, out of all that, the only thing expensive is download.php coming off the disk drive. If you have a PHP Cache of some kind (Zend Cache, et al) then this is cheap. If your OS/disk has a Cache, then this is cheap. If your server gets slammed and download.php isn't in RAM, then it gets expensive. Only stress tests on your server will tell you how expensive it will be, but its' not like the script will take you long to write: ?php session_start(); if (!$_SESSION['authenticated'])) header(Location: http://example.com/login.php;); $filename = $_GET['filename']; //scrub $filename better than this, but it's a start: $filename = basename($filename); readfile(/full/path/to/non/web/storage/area/of/downloads/only/$filenaem); ? That's pretty much it. Change it, test it, stress it, and see if PHP/readfile really slows you down compared to a direct download with no access control at all. I'm betting the answer is No If PHP is too slow, you've still got two good benchmarks to compare other solutions against, and it only took you, what?, a couple hours to develop them? -- Like Music? http://l-i-e.com/artists.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Sending E-Mail - Setting O/S User
On Thu, October 27, 2005 5:04 am, Cabbar Duzayak wrote: When someone sends an e-mail using php, exim sets the following 2 headers (along with others of course): Received: from x.x.x.x (EHLO host.mydomain.com) (x.x.x.x) by mta151.mail.dcn.yahoo.com with SMTP; Wed, 24 Sep 2005 10:29:04 -0700 Received: from O/S User by host.mydomain.com with local (Exim 4.52) id 3ABiAO-00019o-KL for [EMAIL PROTECTED]; Thu, 27 Oct 2005 13:29:03 +0300 Here, the O/S User is picked up from the operation system, and it is the user that owns the process, which is the process that initiates sending out this e-mail. Is there a way to specify a certain user instead of the owner of the current process, or is there a way to configure exim such that, it won't pick up the owner of that process for this header, but a specific user that I explicitly specify through configuration? Maybe. In php.ini, you set the sendmail_path. Your sendmail_path presumably uses exim instead of sendmail? At any rate, if there are command line args for exim to set the From or whatever it is you want changed, you can cram them into the sendmail_path in php.ini, and that changes what happens when mail is sent. If your mail agent does NOT support those as command line args, you could perhaps set up a special configuration of exim for PHP to run that has the right /etc/exim.conf (or whatever) files to make it do what you want. I really only half-understand this with sendmail, and I know zilch about exim, but at least this MIGHT work for you. -- Like Music? http://l-i-e.com/artists.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Detailed Report
On Thu, October 27, 2005 4:26 am, Danny wrote: I´ve got a connection, to a MySQL db, and get the following ResultSet(Category | Name | Code | City) Customers | John | A36 | New York Customers | Jason | B45 | Los Angeles Customers | Max | A36 | Paris Providers | John | A36 | London Providers | Mark | B67 | Madrid You must make sure that your query has: ORDER BY Category in it, and any other ordering (Name, Code, City) comes *AFTER* Category. Otherwise, your Customers and Providers get all jumbled up, and you can't easily separate them. And I need the report in the following format: $last_category = ''; while (list($category, $name, $code, $city) = mysql_fetch_row($result)){ //Only print out Category when we find a new one: if ($last_category != $category){ echo nbsp; $categorybr /\n; $last_category = $category; } echo $name - $code - $citybr /\n; } -- Like Music? http://l-i-e.com/artists.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Mixed PHP/SSI and environment variables
On Thu, October 27, 2005 3:43 am, Christoph Freundl wrote: I have a problem with the persistence of environment variables when mixing PHP and SSI (Apache) and I am not sure if I just made an error or if this approach cannot work at all. So please consider the following files: I *believe* that there are two different scenarios here: 1. Each virtual is a totally separate HTTP request, in the stateless HTTP protocol, and your environment variables in one will not (ever) survive to the next. 2. Apache is configured to do virtual requests as some kind of sub-request and it's all in one single HTTP request/process/thread. I *think* this is a feature I've read about in HTTP specification at http://apache.org If I'm right about all this, none of this has much to do with PHP really, and has everything to do with Apache and configuration. You'd have the same issues in mod_perl or mod_python as you are having in PHP. PS Abondon SSI and just use PHP and your headaches will all disappear. :-) -- Like Music? http://l-i-e.com/artists.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Mixed PHP/SSI and environment variables
On Fri, October 28, 2005 9:36 am, Christoph Freundl wrote: Perhaps I return to what I primarily intended to ask: is it really the wanted behaviour of virtual() that changes that are made by the included file do not influence the environment of the including file? That is most definitely a desirable behaviour for some users. /virtual is a separate HTTP requestm just as if the user had surfed there and crammed in the result. HTTP is stateless for various reasons. There are pros and cons to that, but it was not done on a whim. You MIGHT be able to pass myvar *into* the /virtual as a GET arg... !-- #virtual /whatever.shtml?myvar=!-- #print myvar I'm pretty sure of 2 things: 1. !-- #print myvar -- is probably not how you print out an SSI var. 2. Even if it *IS* how you do it, nesting the !-- # directives ain't gonna work. But you get the idea. -- Like Music? http://l-i-e.com/artists.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Script Multitasking
Hi, I have a file, which I run using command line. The file I am running, runs several files using passthru(). What I realise is, that it runs each file in sequence and waits for its result. I need to run all files at once and they don't have to return the result to the main file. How do I do this? Any help appreciated! Martin Zvarik -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Script Multitasking
On Sat, October 29, 2005 3:57 pm, Martin Zvarík wrote: I have a file, which I run using command line. The file I am running, runs several files using passthru(). What I realise is, that it runs each file in sequence and waits for its result. I need to run all files at once and they don't have to return the result to the main file. How do I do this? Couple Options: Change your script to accept a single filename from the command line ($argv) and then run several of that script from command line in the background: php -q singlefileprocess.php datafile1.txt php -q singlefileprocess.php datafile2.txt php -q singlefileprocess.php datafile3.txt http://php.net/pcntl This basically would be the same (sort of) as just running multiple copies of your PHP script, one for each file, but keeps your script able to process multiple files... Not sure it's worth the effort, but there it is. You could also use http://php.net/exec to have PHP fire up more PHP scripts in the background: ?php //loop through $filename exec(/full/path/to/php -q processonefile.php $filename , $output, $error); if ($error) echo OS Error: $error\n; echo implode(, $output); if ($error) exit; ? Note that in all cases, not only with the results most likely not return to the main file, they'll be reading the files in parallel and doing whatever to them... Be sure that this won't affect other parts of the application. -- Like Music? http://l-i-e.com/artists.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Substr by words
Hello. What do you think about this: ?php $MyOriginalString = This is my original string.\nWhat do you think about this script?; $MaxWords = 6; // How many words are needed? echo substr( $MyOriginalString, 0, -strlen(ereg_replace (^([[:space:]]*[^[:space:][:cntrl:]]+){1,$MaxWords}, ,$MyOriginalString))); ? Only 3 lines. You have to change $MaxWords to 50 if that's what you need. Best regards, Gustavo Narea. Danny wrote: Hi, I need to extract 50 words more or less from a description field. How can i do that?. Substr, cuts the words. Is there any other way to that, without using and array? I mean and implemented function in PHP 4.x I´ve been googling around, but wordwrap, and substr is driving me mad... Thanks in advance Best Regards -- dpc -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Substr by words
If forgot to say that It counts ($MaxWords) words, It doesn't matter if they're separated by simple spaces, line feeds (Unix, dos or mac), tabs, among others. On the other hand, you won't have any problem if you use non-English characters. Best regards, Gustavo Narea. Gustavo Narea wrote: Hello. What do you think about this: ?php $MyOriginalString = This is my original string.\nWhat do you think about this script?; $MaxWords = 6; // How many words are needed? echo substr( $MyOriginalString, 0, -strlen(ereg_replace (^([[:space:]]*[^[:space:][:cntrl:]]+){1,$MaxWords}, ,$MyOriginalString))); ? Only 3 lines. You have to change $MaxWords to 50 if that's what you need. Best regards, Gustavo Narea. Danny wrote: Hi, I need to extract 50 words more or less from a description field. How can i do that?. Substr, cuts the words. Is there any other way to that, without using and array? I mean and implemented function in PHP 4.x I´ve been googling around, but wordwrap, and substr is driving me mad... Thanks in advance Best Regards -- dpc -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Substr by words
Gustavo Narea wrote: If forgot to say that It counts ($MaxWords) words, It doesn't matter if they're separated by simple spaces, line feeds (Unix, dos or mac), tabs, And punctuation marks. Sorry, I'm very forgetful tonight! Cheers. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php