Regular Expressions 101

Community Patterns

Isolate some string (any caracthers) in between isolators, none or many


Regular Expression
PCRE (PHP <7.3)



This expression isolates a string or many strings of any caracthers that are preceded and are followed by a pair of colons in a long text string.

Practical for tooltip insertion. Find a word in the string, search in the database, construct the tooltip and insert within the returned string.

Use with :

  • preg_match() for one unique result.
  • preg_match_all() to get all found strings

$tooltip = preg_match_all( "/::([^.]*?)::/", $string, $matches, PREG_PATTERN_ORDER );

Replace each match with each found $new_string = str_replace( $matches[0], $matches[1], $string );

Submitted by Marc De Gagné - a year ago