Jump to content

Grabbing certain words from a string then put them in an array

Whether you're a seasoned veteran or a struggling beginner, Web Radiance is the web development and web design forum for you. You'll find answers to all your HTML, CSS, SEO, and Programming needs. Pull up a chair and stay awhile.

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

Grabbing certain words from a string then put them in an array Rate Topic: -----

#1 User is offline   Ben Marshall 

  • W.R. Private
  • Group: Members
  • Posts: 31
  • Joined: 26-September 08
  • Gender:Male
  • Location:Dallas, TX
  • Interests:Web Development, SEO

Post icon  Posted 02 April 2009 - 01:57 PM

Alright I've been working on this for a few hours now and need some help. Basically what I have is a string that looks like this:

Ex. "I like [[football]], [[basketball]], and going to the [[park]]."

Now I want to grab the words enclosed with the brackets and put them in an array. I'm completely lost here. I thought about using preg_match but don't know enough about how to use it. If you could make me a simple function that would be great!
0

#2 User is offline   Catalyst 

  • Codesmith
  • Group: Administrators
  • Posts: 1,049
  • Joined: 04-April 06
  • Gender:Male
  • Location:San Diego

Posted 02 April 2009 - 02:53 PM

Yep, regular expressions would be the easiest way to do that. I'd need to see another example or two of input to help you write the expression though.
0

#3 User is offline   Ben Marshall 

  • W.R. Private
  • Group: Members
  • Posts: 31
  • Joined: 26-September 08
  • Gender:Male
  • Location:Dallas, TX
  • Interests:Web Development, SEO

Posted 02 April 2009 - 03:34 PM

View PostCatalyst, on Apr 2 2009, 01:53 PM, said:

Yep, regular expressions would be the easiest way to do that. I'd need to see another example or two of input to help you write the expression though.


Just pretty much what I have above. Like if you could write a function like this I'd really appreciate it:

function keyterms($string) {
   /* Use the regular expresstion here to grab the words */
    $keyterms = array(PUT THE WORDS THAT ARE PULLED FROM THE STRING HERE e.g. "basketball","baseball","park")
    return $keyterms;
}



The words will be enclosed with what I had above "[[", "]]".
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users