Php preg_match simple string examples

November 17, 2010    php dev preg match php function preg_match php

Due to ereg being deprecated in php 5.3.0 + I decided since it was at the core of one of the most heavily used functions on a site of mine, to make the move to preg_match this afternoon. More for my own benefit than anyone elses im just blogging it for future reference.

Some simple preg_matchs I wrote this evening to replace some old ereg functionality.

Firstly a simple match whole string of numeric and alphanumeric characters also including: space, (!), (_), (-), (|) and being between length of 1 and 200 characters. Like I said simples ;)


if(!preg_match('/^[-A-Za-z0-9_!| ]{1,200}+$/',$data)){
return false;
} else {
return $data;
}

Heres another: This time only numeric characters are allowed, with a string length of 1-30.


if(!preg_match('/^[0-9]{1,30}+$/',$data)){
return false;
} else {
return $data;
}

Also like i said more for my own benefit than anyone elses, but feel free to comment if you'd like more examples.


blog comments powered by Disqus