Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Should i Use regexp or string functions

Posted on 2011-02-23
9
376 Views
Last Modified: 2012-05-11
Hi experts!

Please tell me which one is going to be quicker to find and get from string couple of characters.

I want to search through a long string to find match for a youtube id, I don't know where it is going to be. I know the pattern that will occur - youtube.com/v/videoid&...

How do i accomplish this?
What is the fastest way as far as performance is considered?

Many thanks!
0
Comment
Question by:vidda22
  • 4
  • 4
9 Comments
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 34962203
Quick answer: substr is ALWAYS faster

but....

If a pattern is always fixed then using substr will be faster than a regex, but if a pattern is variable then using a regex will be a lot less error prone than messing about with substr.

You should not be worrying about "which one is going to be quicker" because the difference in execution time is tiny - a fraction of a millisecond. Worry about using the right tool for the job instead.

In terms of speed, your MySQL database will have a bigger impact than substr/regex and your customer's ADSL line will have the biggest impact of all because it is usually the slowest component in the whole chain.
0
 

Author Comment

by:vidda22
ID: 34962291
Ok. Thanks bportlock!

Obviously I don't know the videoid.

Could you please tell me how would I take the id using substr?
having this pattern:

.....youtube.com/v/videoid&...


Would substring help me to get couple of these if there were more of them in a string?

If not what code should i use as far as regexp is considered?
0
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 500 total points
ID: 34962367
If you don't know where a substring would turn up in a string (or how many times) then that is a job for a regex, so in your example I would use a preg_match_all and a pattern like '#youtube\.com/v/([^&]+)&amp#s'

UNTESTED

$mystring = ..... string containing what you want to search
preg_match_all( '#youtube\.com/v/([^&]+)&amp#s', $mysting, $matches );
print_r( $matches );

I'm assuming that 'videoId' is where you expect to find the video ID
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 109

Expert Comment

by:Ray Paseur
ID: 34962374
Please post a sample of your test data.  We can help you isolate the information you need.  You may want to use preg_match_all()
http://php.net/manual/en/function.preg-match-all.php

I agree with Brian's characterization that substr() is faster than REGEX.  At the same time, I expect that speed does not matter here.  The CPU is about 100,000 times faster than the disk drives (give or take a few orders of magnitude) so optimizing any purely-CPU operation like string manipulation is milking a mouse.  A lot of effort for very little result.  If you have a performance problem it will always be in the I/O subsystem.  In web sites that means the data base.
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 34962377
0
 

Author Comment

by:vidda22
ID: 34962405
Thanks!
0
 

Author Comment

by:vidda22
ID: 34962505
I used this:

$videoText = '"<http://www.youtube.com/v/YLwZwheyeEc&setting=1>"';

if((bool)preg_match_all( '#youtube\.com/v/([^&]+)&amp#s',$videoText,$matches)){//w{3}\.youtube\.com\/v\/([A-Za-z0-9]{9,12})>$
            print_r($matches);
      }

And result is
Array ( [0] => Array ( [0] => youtube.com/v/YLwZwheyeEc& ) [1] => Array ( [0] => YLwZwheyeEc ) )

I don't want anything else than videoid how do i do that??
so i want to get rid of matches like [0]
0
 

Author Comment

by:vidda22
ID: 34962520
I used this code sorry

$videoText = '"<http://www.youtube.com/v/YLwZwheyeEc&setting=1>"';

if((bool)preg_match_all( '#youtube\.com/v/([^&]+)&amp#s',$videoText,$matches)){
            print_r($matches);
      }
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 34962814
"so i want to get rid of matches like [0]"

$matches[0] is always present, just ignore it. The results are always in $matches[1] onwards depending on the preg options you select
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Wordpress French and English Site 6 77
How to submit record from external php form to a Sharepoint list? 5 55
Link failure 16 32
Wordpress Query 5 25
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question