Semnomic Posted September 20, 2017 Share Posted September 20, 2017 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 ? Link to comment Share on other sites More sharing options...
Sepodati Posted September 20, 2017 Share Posted September 20, 2017 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" Link to comment Share on other sites More sharing options...
Semnomic Posted September 21, 2017 Author Share Posted September 21, 2017 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 !! Link to comment 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. Link to comment 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) Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.