• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 217
  • Last Modified:

php code

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?
  • 2
1 Solution
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
käµfm³d 👽Commented:
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.
Ray PaseurCommented:
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
Ray PaseurCommented:
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now