php code

Posted on 2011-10-07
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
    LVL 48

    Expert Comment

    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 74

    Accepted Solution

    You might try using the "S" modifier ( 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 107

    Expert Comment

    by:Ray Paseur
    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 107

    Expert Comment

    by:Ray Paseur
    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    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 …
    When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now