Converting Posix Regex to PCRE in PHP -


this question has answer here:

i have cms on front-end grabs pages id, shown here:

$pageid = ereg_replace("[^0-9]", "", $_get['pid']); 

this old code trying update since posix deprecated, efforts convert (using preg_replace) have been unsuccessful. if convert line me, appreciate it.

add code comments

my first guess along lines of

$pageid = preg_replace("/[^0-9]/","",$_get['pid'];  

which gave errors, further reduced

$pageid = preg_replace("/^0-9/","",$_get['pid']  

forgive me, understanding of regex rather limited.

let's explain posix pattern does.

[^0-9]

[ start of character class , ] end. when character class starts ^ means it's inverted (= matches listed). 0-9 digits.

so globally [^0-9] matches not digit.

the same pattern available in pcre work:

$page_id = preg_replace('/[^0-9]/', '', $_get['pid']); 

pcre has nice shortcuts express things. example [0-9] can replaced \d (d stands digits) \d (using capital letter) inverse , equivalent [^0-9].

which leads following:

$page_id = preg_replace('/\d/', '', $_get['pid']); 

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -