TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 04-13-2008, 05:36 AM   #1 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default Using substr best method?

I've finally come back to doing a script I was working on awhile back, where originally I was trying to use a regular expression to find the first two paragraph elements in a block of text, and trim everything that occurred after. With all the learning I've been doing since, I've come to the realization that I can do it much faster with a combination of strpos() and substr(), and I'm just wondering if this is the best syntax here, or if there's something I'm missing that could clean it up even further?

What I'm doing is trying to find the second occurence of </p> and strip everything after. So what I did was this:

PHP Code:
$pos strpos($text"</p>"strpos($text"</p>")+4);
$newtext substr($text0$pos); 
strpos() looks for the first occurrence after the specified offset which is why I use it inside of itself to find the first, then use that as the offset to find the second. It clocks in at 0.000061 seconds. The regular expression I was using,

PHP Code:
$newtext preg_replace("~(.*?</p>.*?</p>).*~is""$1"$text); 
clocks in at 0.000095 seconds and seems to me like it could be written better too, but when put against this block of text,

PHP Code:
$text = <<<EOF
<p> this is a test 1 </p>
<p> this is a test 2 </p>
<p> this is a test 3 </p>
EOF; 
it's the only thing I could get to work. Er, wait as I was writing this, I got "~((.+?</p>){2}).*~is" to work as well, yet again it seems like I should be able to write it cleaner.

Either way, strpos() still seems to be the fastest method. Any ideas on better ways?
-m
delayedinsanity is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 09:33 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design