Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 13th, 2006, 5:21 AM   #11
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
A one-line solution does not imply an efficient solution. Regex is rarely the proper choice when a substr will do.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Dec 13th, 2006, 10:56 AM   #12
headzoo
Newbie
 
Join Date: Oct 2006
Posts: 16
Rep Power: 0 headzoo is on a distinguished road
Quote:
Originally Posted by DaWei View Post
A one-line solution does not imply an efficient solution. Regex is rarely the proper choice when a substr will do.
DaWei, I agree 100%. If I had a choice between:

echo preg_replace('~\n~', '<p/>', $str);

and

echo str_replace("\n", '<p/>', $str);

I would take the latter every time. However in Intimidat0r's case, he's trying to come up with a complicated, difficult to maintain solution, when a simple regexp would do.

Furthermore, if Intimidat0r is new to PHP, he may not have been exposed to regexp yet, and IMHO regexps are one of the handiest tools for any developer to have in his toolbox.

- Sean
headzoo is offline   Reply With Quote
Old Dec 13th, 2006, 11:12 AM   #13
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
You're correct about the utility of regex, of course. I have an application written in Python and in PHP. I used regex in one and substr in the other because I knew the regex wasn't a critical penalty. I'm just trying to point out that the one-liner regex is rife with loops and emits one helluva chunk of code. That isn't necessarily a characteristic that's picked up on by a novice.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Dec 13th, 2006, 11:17 AM   #14
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by DaWei View Post
A one-line solution does not imply an efficient solution. Regex is rarely the proper choice when a substr will do.
Whilst I agree generally, I'm not sure this applies in this particular case. PHP is an interpreted language, so you're going to have to do some parsing, regardless, and a regular expression is going to be faster to interpret than a PHP. Regarding execution time, I'm not sure you'd see considerable difference between a function that uses multiple conditions, loops, and substr calls, and a one-line regular expression executed by a binary regex parser. Indeed, a regular expression may even be faster.
Arevos is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
What is this 'if __name__ == '__main__'' thing? commodore Python 2 Apr 30th, 2006 3:46 PM
Last thing you had for supper. Eric the Red Coder's Corner Lounge 22 Apr 17th, 2006 11:02 AM
algorithm of fast exponentiation MicRo C++ 1 Mar 12th, 2006 5:29 PM
SpcFileWipe algorithm in Secure Programming Cookbook not working c0ldshadow C++ 1 Aug 7th, 2005 7:40 PM
rsa encrption decrption algorithm justdoit C 2 May 4th, 2005 9:16 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:37 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC