Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 229
  • Last Modified:

preg_match

I use the below preg_match

preg_match_all("/\b\b(.+?)\bindia\b(.+?)\b\b/i",$content,$contentarray)


$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?
0
jacobbbc
Asked:
jacobbbc
  • 2
  • 2
1 Solution
 
käµfm³d 👽Commented:
What do you expect to capture in $contentarray?
0
 
Carl BohmanCommented:
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.
0
 
Carl BohmanCommented:
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:

/^.*?(\S*)\s*\bindia\b\s*(\S*)/

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/.
0
 
jacobbbcAuthor Commented:

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.
0
 
käµfm³d 👽Commented:
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.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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