TheBrandon Posted November 1, 2010 Share Posted November 1, 2010 Hello all, I need to switch my code from ereg_replace to preg_match but I'm having trouble figuring out how to get the syntax right for this and keep the same functionality. $vendor_ID = ereg_replace("^\*","",$key); Any advice on how to maintain this functionality but use non-deprecated methods? Quote Link to comment https://forums.phpfreaks.com/topic/217462-switching-from-ereg_replace-to-preg_match-syntax-help/ Share on other sites More sharing options...
salathe Posted November 1, 2010 Share Posted November 1, 2010 See http://php.net/reference.pcre.pattern.posix (especially point 1 in the list). Quote Link to comment https://forums.phpfreaks.com/topic/217462-switching-from-ereg_replace-to-preg_match-syntax-help/#findComment-1129035 Share on other sites More sharing options...
TheBrandon Posted November 1, 2010 Author Share Posted November 1, 2010 Yeah I've tried these: $subcat_ID = preg_match("\^\*\","",$key); $subcat_ID = preg_match("\^\*\",$key); $subcat_ID = preg_match("/^\*/","",$key); $subcat_ID = preg_match("/^\*/",$key); They all generate errors. That's why I'm asking for help. Quote Link to comment https://forums.phpfreaks.com/topic/217462-switching-from-ereg_replace-to-preg_match-syntax-help/#findComment-1129036 Share on other sites More sharing options...
Pikachu2000 Posted November 1, 2010 Share Posted November 1, 2010 Are you sure you didn't mean to use preg_replace() rather than preg_match()? Quote Link to comment https://forums.phpfreaks.com/topic/217462-switching-from-ereg_replace-to-preg_match-syntax-help/#findComment-1129058 Share on other sites More sharing options...
TheBrandon Posted November 1, 2010 Author Share Posted November 1, 2010 Oh snap, you're right. I still need help with the syntax though. I'm still learning expressions like this. I've tried: $vendor_ID = preg_replace("^\*","",$key); $vendor_ID = preg_replace("\^\*","",$key); $vendor_ID = preg_replace("\^\*\","",$key); None seem to work? Quote Link to comment https://forums.phpfreaks.com/topic/217462-switching-from-ereg_replace-to-preg_match-syntax-help/#findComment-1129087 Share on other sites More sharing options...
AbraCadaver Posted November 1, 2010 Share Posted November 1, 2010 So you're trying to replace an * at the beginning of the string? for simple stuff, the only modification to an ereg pattern to get to PCRE is to add delimiters. Here I use / $vendor_ID = preg_replace("/^\*/", "", $key); Quote Link to comment https://forums.phpfreaks.com/topic/217462-switching-from-ereg_replace-to-preg_match-syntax-help/#findComment-1129091 Share on other sites More sharing options...
salathe Posted November 1, 2010 Share Posted November 1, 2010 preg_replace("/^\*/" ... will work. You were close earlier, just using the wrong function. Please take the time to read through the documentation (start here: PCRE functions) to familiarise yourself with this new set of functions. Quote Link to comment https://forums.phpfreaks.com/topic/217462-switching-from-ereg_replace-to-preg_match-syntax-help/#findComment-1129093 Share on other sites More sharing options...
AbraCadaver Posted November 1, 2010 Share Posted November 1, 2010 Oh, but this would be quicker for your simple example: $vendor_ID = ltrim($key, '*'); Quote Link to comment https://forums.phpfreaks.com/topic/217462-switching-from-ereg_replace-to-preg_match-syntax-help/#findComment-1129096 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.