httpremix.com

About the Author

Jonah Ellison lives in Seattle, Washington and works at a web firm developing websites and custom web applications for the LAMP solution stack. He enjoys optimization, front-end usability, databases, clean code and clever solutions. Contact him or learn more.

 

Topics

php

PHP: Parsing a string from text 2008 Jun 10

Topics: php — by jonah ellison

What if you need to grab a string from text, such as the name of the department below:

Position #: 08-26
Position: Lab Assistant II
Department: Black Mesa Research Facility
Status: Full-time

Here's a quick way to pick out that string:

  1. $match = preg_match_all("/Department:(.*)/", strip_tags($body), $matches, PREG_SET_ORDER);
  2. $deptName = trim($matches[0][1]);