Jump to content

[SOLVED] New Coder arguing with regex


seopaul

Recommended Posts

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  :)

Link to comment
https://forums.phpfreaks.com/topic/54052-solved-new-coder-arguing-with-regex/
Share on other sites

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.

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  8) 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  :)

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.