4 Replies - 500 Views - Last Post: 12 May 2011 - 01:05 PM Rate Topic: -----

#1 Misfit  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 100
  • Joined: 18-July 09

regular expressions

Posted 11 May 2011 - 02:59 PM

For string output like:

* other
** recentchanges-url|recentchanges
** randompage-url|randompage

I'm attempting to grab just the "recentchanges" and "randompage" strings. However, I'm not very good at regular expressions, so this task has given me a headache for a good hour now.

Currently, my code splits the string by merely the asterisk:
var_dump(preg_split('/\*/',$wtext,null,PREG_SPLIT_NO_EMPTY));



My earlier attempt was something like:
/\*[a-ZA-Z0-9_\-.]+\|/

The full output that I'm trying to split:
* technical
** google_SEO|SEO / Web Site info
** useful|Useful Stuff
** scripts|Scripts

* other
** recentchanges-url|recentchanges
** randompage-url|randompage

This post has been edited by Misfit: 11 May 2011 - 03:03 PM


Is This A Good Question/Topic? 0
  • +

Replies To: regular expressions

#2 Misfit  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 100
  • Joined: 18-July 09

Re: regular expressions

Posted 11 May 2011 - 03:33 PM

I'm now following the tutorials at http://www.dreaminco...essions-part-2/ - which are helping. (At least I'm learning why my old code isn't working - something my textbook couldn't explain)

If you guys know of any other tutorials that would help, I'm down for those kinds of posts too.
Was This Post Helpful? 3
  • +
  • -

#3 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 947
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Re: regular expressions

Posted 11 May 2011 - 03:57 PM

This explains the PCRE functions. This one's also a nice one.

But I recommend this book -> http://ptgmedia.pear...47149X_book.pdf which explains how to use the functions and also reg expressions.
Was This Post Helpful? 3
  • +
  • -

#4 Misfit  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 100
  • Joined: 18-July 09

Re: regular expressions

Posted 12 May 2011 - 12:42 PM

View Postcodeprada, on 11 May 2011 - 04:57 PM, said:

This explains the PCRE functions. This one's also a nice one.

But I recommend this book -> http://ptgmedia.pear...47149X_book.pdf which explains how to use the functions and also reg expressions.


Thanks. In the end, I ended up not using regex's at all. But I am practicing them so I'll be able to in the future.

My solution (in case anyone was curious):

foreach($array as $key => $value){
   $position = strrpos($value, "|");
   //if no pipe is found
   if ($position === FALSE){
     unset($array[$key]);
     continue;
   }
	
   $newlinewords = substr($value, $position+1, -2);
}

This post has been edited by Misfit: 12 May 2011 - 12:45 PM

Was This Post Helpful? 0
  • +
  • -

#5 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 947
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Re: regular expressions

Posted 12 May 2011 - 01:05 PM

PCRE is quite easy once you've grasp the concepts. Like for instance you wanted to find out if a | exists or not.
preg_match('#|#', $value)

This post has been edited by codeprada: 12 May 2011 - 01:06 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1