newb Posted August 5, 2010 Share Posted August 5, 2010 $text = preg_replace('{-(\d+)}e', '-convertNumber("$1")', $text); i have strings im trying to replace like this: name-2 > name-two name-p2 > name-p2 im trying to match and replace the first one and basically ignore the second one. ive included the hyphen in the code. so why isnt this working? Link to comment https://forums.phpfreaks.com/topic/209911-preg-replace-not-working/ Share on other sites More sharing options...
JasonLewis Posted August 5, 2010 Share Posted August 5, 2010 Try using a negative lookbehind to make sure that any digits are not preceeded by any alphanumeric characters. $text = preg_replace('{(?<!\w)(\d+)}e', 'convertNumber("$2")', $text); Not tested. Link to comment https://forums.phpfreaks.com/topic/209911-preg-replace-not-working/#findComment-1095648 Share on other sites More sharing options...
newb Posted August 5, 2010 Author Share Posted August 5, 2010 ok, and how do i check if it contains alphanumeric chars after it: for example name-2nd should stay as name-2nd Link to comment https://forums.phpfreaks.com/topic/209911-preg-replace-not-working/#findComment-1095660 Share on other sites More sharing options...
JasonLewis Posted August 5, 2010 Share Posted August 5, 2010 Use a negative lookahead. $text = preg_replace('{(?<!\w)(\d+)(?!\w)}e', 'convertNumber("$2")', $text); Link to comment https://forums.phpfreaks.com/topic/209911-preg-replace-not-working/#findComment-1095663 Share on other sites More sharing options...
newb Posted August 5, 2010 Author Share Posted August 5, 2010 worked perfectly, thanks so much! Link to comment https://forums.phpfreaks.com/topic/209911-preg-replace-not-working/#findComment-1095665 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.