seopaul Posted June 3, 2007 Share Posted June 3, 2007 hi, im quite new to php putting my first php site together, one of my books has a really nice function. it scans text to find words in the text that match a set word list. at the moment to get the regex working the word needs to be formatted like "[this]" i have tired to get the regex to word with out [ ] around the word without much luck. i was wondering if someone could explain what im doing wrong with this bit of regex. preg_match_all('/\[([^\]]+?)\]/', $page, $matches, PREG_SET_ORDER finds words in "[ WORD MATCH HERE ]" i tred removing " \[" and "\]" preg_match_all('/([^\]]+?)/', $page, $matches, PREG_SET_ORDER but that doesn't work. any ideas? thanks in advance Quote Link to comment Share on other sites More sharing options...
seopaul Posted June 4, 2007 Author Share Posted June 4, 2007 if it helps the regex gives two outputs the word with the [ ] and the same word without so [word] + word Quote Link to comment Share on other sites More sharing options...
Wildbug Posted June 5, 2007 Share Posted June 5, 2007 So you are NOT trying to find words in brackets? What's your wordlist? Quote Link to comment Share on other sites More sharing options...
LuAn Posted June 5, 2007 Share Posted June 5, 2007 I'm no expert but it looks to me like you are barking up the wrong tree. Here's a breakdown of that first regex as I understand it [pre] '/ \[ - match an opening square bracket ([^\]]+?) - match any number of characters that are NOT a closing square bracket \] - match a closing square bracket /' [/pre] Thus what you have done is: [pre] '/ ([^\]]+?) - match characters that are not a closing square bracket /' [/pre] \b denotes a word boundary so I think what you need is: '/\b([a-z]+)\b/i' which as I understand it breaks down as: [pre] '/ \b - match a word boundary ([a-z]+) - one or more alpha characters \b - match a word boundary /i' - the i here makes it case insensitive [/pre] If you are happy to match any alphanumeric character as opposed to just a-z you could use the \w special character and use: '/\b(\w+)\b/' At least that's how I understand it. Quote Link to comment Share on other sites More sharing options...
seopaul Posted June 8, 2007 Author Share Posted June 8, 2007 your right LuAn i am barking up the wrong tree, thanks to your post i realised that i need to change the script abit. your regex worked perfectly but the script displayed the info sstrangley which mademe realise where i had gone wrong. in case your wondering im putting a small litght weight wiki as all the ones ive found have far to many features for my purpose. at the moment the regex find words [like this] and then displays a link to a wiki page. now realising this it's quite a dumb system as it will display a link regardless of if the page exist's, i think i should change the data store to a sql database, so thank you LuAn that bit of regex help should work well Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.