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
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.

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.