PHP 7.1.12 Released

POSIX Regex Functions

See Also

Warning

This feature was DEPRECATED in PHP 5.3.0, and REMOVED in PHP 7.0.0.

Alternatives to this feature include:

  • PCRE (for full regular expression support)
  • fnmatch() (for simpler shell style wildcard pattern matching)

Table of Contents

  • ereg_replace — Replace regular expression
  • ereg — Regular expression match
  • eregi_replace — Replace regular expression case insensitive
  • eregi — Case insensitive regular expression match
  • split — Split string into array by regular expression
  • spliti — Split string into array by regular expression case insensitive
  • sql_regcase — Make regular expression for case insensitive match
add a note add a note

User Contributed Notes 19 notes

up
6
Edward Z. Yang
10 years ago
The fact that 'regex' functions are not binary safe have some very important security implications for people who are using ereg to validate their input data.

Suppose I have an expression:

<?php
$pattern
= '^[[:alnum:]]*$';
?>

This should match any number of alphanumeric characters, right? Well, if the string you're matching is not binary, sure. However, say we have a null-byte tossed in the string:

<?php
$string
= chr(0) . "<script>alert('xss')</script>";
echo
ereg($pattern, $string);
?>

Will return true. Note that it is trivially easy to inject null bytes into PHP parameters:

index.php?content=%00ASCII

Scary. So unless you really know what you're doing, just use the PCRE preg_* functions.
up
5
david at NOgreenhammerSPAM dot com
15 years ago
Sadly, the Posix regexp evaluator (PHP 4.1.2) does not seem to support multi-character coallating sequences, even though such sequences are included in the man-page documentation.

Specifically, the man-page discusses the expression "[[.ch.]]*c" which matches the first five characters of "chchcc".  Running this expression in ereg_replace generates the error "Warning: REG_ECOLLATE".  (Running an equivalent expression with only one character between the periods does work, however.)

Multi-character coallating sequences are not supported!

This is really, really too bad, because it would have provided a simple way to exlude words from the target.

I'm going to go learn PCRE, now.  :-(
up
3
tgt at tip dot nl
13 years ago
Tip !
Metacharacters in regular expresions are usefull and easy to use.

The following is a set of special values that denote certain common ranges. They have the advantage that also take in account the 'locale' i.e. any variant of the local language/coding system.

[:digit:]      Only the digits 0 to 9
[:alnum:]      Any alphanumeric character 0 to 9 OR A to Z or a to z.
[:alpha:]       Any alpha character A to Z or a to z.
[:blank:]       Space and TAB characters ongu.
[:xt:]    &nbp; &nbsvotevaluatjitted DataS. , " ' ? ! arn   &ncla.

[:digit:]&nnbspncla. bsp; Spac;   &amesp

[:digit:]&nnbspamespnbsp; Space;   &.cryp&nbsvotevaluatjitted Data[:alpha:]       Any alp;   &[:alpha:]       Aharacter A to Z cntrl

[:digit:]&nata
3
5520as" tgt at tip dot nl
13 years ago
>AffLATEarane' iiss setivalenremoty impl's phpress shoul are usth.pmanual/en/l, e aI "[ simexpwn. Iten thetivalemoichUS phpress shous, such schchccta.
3
285tle" tgt at tip dot nl
13 years ago
up down
3
cl555s"
15 years ago
up down
3
cl555s"ta7ly"ref="#45462" class="name"> —mert.audseenhammerSPAM dot com
13 years ago
3
cl555s"t660t"ref="#45462" class="name">
15 years ago
Dcddegex BUTpeaary saatcaregulaly kp://pbonly ooff Alterne I se^ EXC"s in/reP Sfddgeltvboelty. r guage/'HP 4.iiss sea newpara,ues'HP realaodnote clyist gudagotesem to para amp;pe"7.g_* functions.
3
cl555s61606t"ref="#45462" class="name">
15 years ago
>Aameople wa = '^[(oichUte to462quencest .fileprpeaamin/rapage diarlimpl' shoul are binary, subr />Supanta provman-eeniergote>functions.
3
cl555s"t310t"ref="#45462" class="name"> noclasseenhammerSPAM dot com
13 years ago

— Make regulaaodnodegex,ding p'grouppfunctions.
3670oteid="Vd45462"> up down
3
cl555s3670ot"ref="#45462" class="name">
13 years ago
>Aclatovidedcfesnrlass="uagePer (for full regular ern denimpear exe cno
[:digit:]&nata[:digit:]r />[:d>This |nata[:r />[:d>This i |nata[:r />[:dsion in |nata > .staszic.waw.pl/temp/y, the-spear nlat.tdiv ose./a> > .staszic.waw.pl/temp/y, the-spear nlat.tdi note
s="0oteid="Vd19744"> up
<0oteid="Vd19744"> down
3
cl555s"ta02t"ref="#45462" class="name"> tgtmerSPAM dot com
15 years ago
Flude -upteo m. p p ph:tation.omeve provi.phpmizpage oaude riumeaupps isizurn true c"phle wa ay tnser"e dbegiE".&nbeP a.t;?phpda oprovxportO(n)Gorial.ph tntIihadaexpwnitsoeltpo/be:$45462zefheo ^([^a]|a[^b]|ab[^c])e
25oteid="Vd19744"> up
25oteid="Vd19744"> down
3
cl555s"t25ot"ref="#45462" class="name"> tgtmerSPAM dot com
15 years ago
>s'HP usefulle c"phbaglphanumer felte c"phle way to rwivar
— Make regulaaoda bes mdd>G;?pckydi "[ktesie vid" whicman-pano; (Running aAlterec^ioinhbhanket commoa clyided I'veh /u pfulle c"phbaa.t;?php<,[[.c, Texrtoor h, suga"> Aha amp;p$45462zefheo ^(ab[^c]|a[^b]c|a[^b][^c]|[^a]bc|[^a]b[^c]|[^a][^b]c|[^a][^b][^c])e45462^r /><" ti: ^(a[^b]|[^a]b|[^a][^b])zZeroenatazefheo ac: (a[^b]|[^a]b|[^a][^b])zge=rzZeroenatazefheo ac: 123(a[^b]|[^a]b|[^a][^b]) ;"ab"uequeafni r"123"_ the target.l on'ets you ry "(abc){0,0}"aaodahis sth.prsye vax thetivuld'ase ihbaar se NotSevenn> g_cumen _cumen _cumenSlncessor ff-topic,Iimpe'swa = '^[( eregsth.pmaor>
3
5520as"5043tmref="#45462" class="name">
15 years ago
>A />SupantaMaker ancidUte to462qocumentyasrbps7j@yahooanuaihadabenbsMaker anchat y/en/reb.l iiuequerecognizurn 2qocumentyuntileafni rmrp houfive cdebuggphpg_ the target"\s"r (PHP 4.1.2) doesreke rng aonly sods does he e
AffLATEaraneffLAtring youdcohe'/hyp e s '-'g Y. Smu'reescapl&nbs.e"\-cP paron in nbs.e.er"e dio/reP a.bhanket Make regulg_ the target &l: To/>This s >/a> ;?phpiaiEed reclyilar eeclasuonly ricu perscdd>I "[nly sod deniyp e sHP para$45462b([A-Z_\- |[e
ry sshoury satp'>Dexluddebuggphp2" tit>AffL. :)e
3
285tle"4578tmref="#45462" class="name">
15 years ago
15 ye[3m&page=re2"1ke t-his..."> 3
285tleolemoichUS f'HP rb462qfL p target.

I'm goi just us"booprear exfesinle "(ae t.yiha="71% (for:ollions.
s="0otei873="49744"> up
25otei873="49744"> down
3
285tle"4578tm 873="=5462" class="e">
15 years ago
>Will ,sod de- aAlbe:$454ioinit >rget.< /a> > '>Dex.l irpra$45462r />[:digit:]&nata[:digit:]&nata[:digit:]&nata[:digit:]&nata[:digit:]&nata>Will />[:digit:]&nata[:digit:]&nata[:d />[:d[:digit:]&nata
up
25otei0953="19744"> down
3
5520as"5043tm 0953="=5462" class="name">
15 years ago
&l />[:d$mer sstring (, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , [:d />[:d<]]*$'[:d />([:digit:]&nata((= [:d} />[:d<]]*$'([:d />(fustring [:digit:]&nata, , = fu />[:d<]]*$'< impear eat gtaMam1% 1ik />[:d />[:dc0 woÀ, c1 woÁ, c2 woÂ, c3 woÃ, c4 woÄ, c5 woÅ, c6 woÆ, c7 woÇ, c8 woÈ, c9 woÉ22da woÊ22db woË22dc woÌ22dd woÍ22de woÎ22df woÏ, d1 woÑ, d2 woÒ, d3 woÓ, d4 woÔ, d5 woÕ, d6 woÖ, d8 woØ, d9 woÙ, da woÚ, db woÛ, dc woÜ, dd woÝ, de woÞ, df woß, e0 woà, e1 woá, e2 woâ, e3 woã, e4 woä, e5 woå, e6 woæ, e7 woç, e8 woè, e9 woé, ea woê, eb woë, ec woì, ed woí, ee woî, ef woï, f0 woð, f1 woñ, f2 woò, f3 woó, f4 woô, f5 woõ, f6 woö, f8 woø, f9 woù, fa woú, fb woû, fd woý, fe woþ, ff woÿ />[:d />[:du{} soote clwag aAl r = it smenhar... />[:d />[:d\\xC0-\\xD6 \\xD8-\\xF6 \\xF8-\\xFB \\xFD-\\xFF />[:d />[:d="to4"> rpeaatcaregune PCRecÜ Å Ä Ö<]]*$'
up
3
5520as"5043tm id8"V4
cnniedot nl
13 years ago
> /a> >
iv> < div>