![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#12 | |
|
Newbie
Join Date: Oct 2006
Posts: 16
Rep Power: 0
![]() |
Quote:
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 |
|
|
|
|
|
|
#13 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#14 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
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.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |