php code

Posted on 2011-10-07
Medium Priority
Last Modified: 2012-08-13
When I use the below preg_match

It will display output if match is found.Otherwise if no match is found it will take more  than five seconds to display null array. How I can make the execution fast?
Question by:jacobbbc
  • 2
LVL 48

Expert Comment

ID: 36930475
What do you want to extract from the string?

The problem and peformance issu of your regexp is \b(.+?)\b which can man anything with atleast one char. The dot does not fullfill the word boundery condition (\b) and thus every char after com is tested for the match

To improve performance replace \b(.+?)\b with a suitable expression without the dot
LVL 75

Accepted Solution

käµfm³d   👽 earned 2000 total points
ID: 36930517
You might try using the "S" modifier (http://www.php.net/manual/en/reference.pcre.pattern.modifiers.php). There matching will be slow the first time the pattern is used, but subsequent uses of the same pattern *should* run a tad faster.


Open in new window

I also think your pattern could be cleaned up a bit. In order to do that, though, we would need to see sample input and expected output.
LVL 111

Expert Comment

by:Ray Paseur
ID: 36931164
replace \b(.+?)\b with a suitable expression - No kidding!  

We do not really add any value by looking at non-working code.  The test data and a clear vision of the desired output is the key to success.

Here is what we need you to do for us.  Post a test input string.  It does not have to be elaborate, just something representative of what you expect to receive in your input.  Then post a desired output string which will be a subset of the  input string.  And tell us in plain, non-technical language what the rules are for getting from the input string to the output string.  Then we will be able to be much more helpful.

Example: I want to find the first word that starts with "q"
Inputs: The quick brown fox
Output: quick

For that we might come up with a regex something like this: $rgx = '#\bq.*?\b#i';

Thanks and regards, ~Ray
LVL 111

Expert Comment

by:Ray Paseur
ID: 37045297
I would be very interested to see the before and after benchmarks.  Please post the sample input test data you used so we can see the results on our own systems, thanks.

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses
Course of the Month16 days, 7 hours left to enroll

862 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