Semnomic Posted September 20, 2017 Share Posted September 20, 2017 (edited) Hi Im struggling with this problemI have a string, in this string I have a "-" followed by 10 random digits. so eg. $mystring = cat-345-4345645678abcd-12abcI can find the "-4345645678" with ([-][0-9]{10})|. I need to replace the "-" with an "xx"the "-" I'm trying to replace MUST be followed by ten random numbers.Any ideas ? Edited September 20, 2017 by Semnomic Quote Link to comment https://forums.phpfreaks.com/topic/305063-regex-question-find-pattern-and-replace-part-of-pattern/ Share on other sites More sharing options...
Sepodati Posted September 20, 2017 Share Posted September 20, 2017 (edited) Like this? <?php $mystring = 'cat-345-4345645678abcd-12abc'; $newstring = preg_replace('/-([0-9]{10})/', 'xx$1', $mystring); var_dump($newstring); ?> Output: string(29) "cat-345xx4345645678abcd-12abc" Edited September 20, 2017 by Sepodati Quote Link to comment https://forums.phpfreaks.com/topic/305063-regex-question-find-pattern-and-replace-part-of-pattern/#findComment-1551677 Share on other sites More sharing options...
Semnomic Posted September 21, 2017 Author Share Posted September 21, 2017 (edited) Yes thanks, never used or seen var_dump so unsure on the way it outputs Output: string(29) "cat-345xx4345645678abcd-12abc" ?? Thanks I will read up !! Edited September 21, 2017 by Semnomic Quote Link to comment https://forums.phpfreaks.com/topic/305063-regex-question-find-pattern-and-replace-part-of-pattern/#findComment-1551678 Share on other sites More sharing options...
Sepodati Posted September 21, 2017 Share Posted September 21, 2017 It's just a detailed print. More useful for arrays, but I used it out of habit. Quote Link to comment https://forums.phpfreaks.com/topic/305063-regex-question-find-pattern-and-replace-part-of-pattern/#findComment-1551680 Share on other sites More sharing options...
requinix Posted September 21, 2017 Share Posted September 21, 2017 Another way is to make a regex that finds a "-" but only if it's followed by 10 digits. preg_replace('/-(?=[0-9]{10})/', 'xx', $mystring) Quote Link to comment https://forums.phpfreaks.com/topic/305063-regex-question-find-pattern-and-replace-part-of-pattern/#findComment-1551685 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.