regex patterns for php

You are here

Regex can be so difficult but because of its ability to be so flexible it becomes necessary to use when searching a string that could have multiple variants. PHP uses the PCRE format so this could be used in other formats. Below are patterns I constantly use,

 

Searching for a characters with tags in a string

// String to be searched
1111<thumbnail code='1150400' />2222 

// Regex pattern
/<thumbnail code='.+?' \/>/

// Response
<thumbnail code='1150400' /> 

Searching for the code in a string

// String to be searched
1111<thumbnail code='1150400' />2222 

// Regex pattern () is what you will search for
/<thumbnail code='(.+?)' \/>/ 

// Response
1150400