Posted on 2011-10-12
Last Modified: 2012-05-12
I use the below preg_match


$content=indiatimes india news business movies cricket shopping more indiatimes the times of india the economic times more try out our new home page and give us your feedback web indiatimes shopping mobile advanced search make indiatimes my home page your feedback our new sites timesdeal gaana hutk kambola superstud emotional atyachaar 3 healthmeup luxpresso guylife radio mirchi communities itimes chat itimes polls speaking tree news the times of india the economic times epaper hotklix times crest x907 x915 x928 x949 x92e x93f x915 x91f x93e x907 x92e x94d x938 xa88 xa95 xacb xaa8 xacb xaae xabf xa95 xa9f xabe xa88 xaae xacd xab8 live tv 2344 2357 2349 2366 2352 2340 x91f x93e x907 x92e x94d x938 x92e x939 x93e x930 x93e x937 x94d x91f x94d x930 x91f x93e x907 x92e x94d x938 infotech 2311 2344 2381 2347 2379 2335 2375 2325 nba ipl car news reviews cricket blogs leisure femina miss india timescity contests photogallery movies astrology online games idiva wine more absolute radio videos picture stories golfingtimes shop mobiles books shopping buy car mobile 58888 contest ringtones 58888 voice mocolife mobile connect services times content jobs marry property book an ad biz solutions book your domain etesting nri solutions new draft telecom policy some key proposals of the telecom policy that is expected to be finalised by december related links most expensive mobile phones all about kapil sibal all about trai not just any performer jagjit sang from the heart and he became the song and the poetry contained therein related links end of an era jagjit singh 1941 2011 jagjit singh wooed with his melodic baritone amitabh bachchan 69 the actor is on a mission to connect with as many people as possible related links indian waxworks at madame tussauds bollywood s best teachers all about amitabh bachchan sallu s next in 5 countries ek tha tiger will be shot in five countries according to director kabir khan related links bollywood s best bodyguards make up time for bollywood top bollywood item boys stars at 17 17 was a magical age when responsibility hadn t yet crept over says neha dhupia related links vroom dhoom the bald and the beautiful b wood s no smooch actors wardrobe essentials from jeans and ties to watches here are some wardrobe essentials for men related links best dressed men fit fitter bollywood superstitious bollywood red bull goes sky high red bull driver neel jani drives f1 car at the highest motorable road in the world related links hyundai eon customers speak skoda s new model offensive mahindra xuv5oo registers over 5 000 bookings best of topless bollywood these leading actresses and item girls had no problem posing with their tops off related links win f1 party passes and meet lewis hamilton world s most influential businesswomen battle of the sexy young sirens genelia vs riya healthier breakfasts read about the 6 steps to a healthier breakfast today related links surprising facts about exercise sweat how to get better sleep healthy dinner recipe tandoori chicken latest entry level androids androids for the budget conscious sporting 1 ghz processors and the latest firmware related links telecom policy promises nation wide free roaming microsoft touch mouse makes it to india nokia wp7 phones gathering steam haute destination florence florence in italy is now an opulent vacation hot spot related links india s smallest coolest luxury hotel world s top secret islands foodie

Here india is the checking keyword and it can be changed by another keyword.For india results are obtained fast but when i give the keyword 'com' which is not in $content it takes more than five seconds as execution time.How can i make it fast?
Question by:jacobbbc
    LVL 74

    Expert Comment

    by:käµfm³d 👽
    What do you expect to capture in $contentarray?
    LVL 13

    Expert Comment

    by:Carl Bohman
    The biggest contribution to the time is likely the fact that you are not anchoring the regex using ^.  This means that the regex will attempt to match at each and every character of the content until a match is found or until it reaches the end of the string.  This is unlikely to be what you want to happen.

    Another problem is that a word boundary match (\b) is zero-width.  No matter how many of those you put next to each other, they will all match the exact same word boundary.  Removing one of them from the double match (\b\b) might result in a small gain if the code is executed repeatedly.

    The regex, as it is written, can be described as:
    1. Match a word boundary (which might be the start of the string, but could be the end or beginning of any word in the entire text).
    2. Minimally match all characters until the keyword is found (which can include word boundaries and whitespace).
    3. Match the keyword.
    4. Minimally match at least one character after the keyword (which will always be a space character in your case, unless the keyword is at the end of the string with no space following it, in which case the regex won't match at all).

    I doubt this is really what you are trying to match.
    LVL 13

    Expert Comment

    by:Carl Bohman
    If I were to guess what you are trying to do, I'm guessing that you are trying to match a given keyword and extract the two keywords found on either side.  If so, here's a regex to do that:


    Open in new window

    If instead you are looking to capture the entire string before and after the keyword, I would suggest that preg_split be used with the regex /\bindia\b/.

    Author Comment


    I have page content of a website I have to make a short description from that that content. So I use this preg_match to retrieve some words before the keywords in an array named $contentarray.
    LVL 74

    Accepted Solution

    So I use this preg_match to retrieve some words before the keywords in an array named $contentarray.
    OK. Try this:

    $keyword = "india";
    preg_match_all("/(?:\w+(?: |\b)){1,10}\b$keyword\b/i", $content, $contentarray);

    Open in new window

    You can alter the values of "1" and "10" to give you a different count of words. This pattern only looks for words before the keyword, per you last statement.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
    As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    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…

    758 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

    13 Experts available now in Live!

    Get 1:1 Help Now