redfox180 Posted January 21, 2008 Share Posted January 21, 2008 Hi, if I have some text like this: PD32TR/PD54YT/PD43QW/512RE/51QW/PD43WQ/... I would like to ad PD to all those ones who haven't. Any suggestion??? Keep in mind that the dispositions can change. Thanks James Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted January 21, 2008 Share Posted January 21, 2008 Try something like this: $parts = explode('/', $string); foreach($parts as $key => $part) { if(substr($part), 0, 2 != 'PD') $parts[$key] = 'PD' . $parts[$key]; } $string = join('/', $parts); Quote Link to comment Share on other sites More sharing options...
redfox180 Posted January 21, 2008 Author Share Posted January 21, 2008 It doesn't seem to work... Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted January 21, 2008 Share Posted January 21, 2008 Well, I just misplaced a ). Another time though, please state how "it isn't working". <?php $string = 'PD32TR/PD54YT/PD43QW/512RE/51QW/PD43WQ'; $parts = explode('/', $string); foreach($parts as $key => $part) { if(substr($part, 0, 2) != 'PD') $parts[$key] = 'PD' . $parts[$key]; } $string = join('/', $parts); echo $string; // output: PD32TR/PD54YT/PD43QW/PD512RE/PD51QW/PD43WQ ?> Quote Link to comment Share on other sites More sharing options...
redfox180 Posted January 21, 2008 Author Share Posted January 21, 2008 Sorry Daniel, I realized it only ofter I posted it... Thanks! Quote Link to comment Share on other sites More sharing options...
resago Posted January 21, 2008 Share Posted January 21, 2008 $string=preg_replace('/\/(/d/d..)\//','/PD$1/',$string); Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted January 21, 2008 Share Posted January 21, 2008 $string=preg_replace('/\/(/d/d..)\//','/PD$1/',$string); That will result in an E_WARNING. I see what you're trying to do though, but it won't work if the item is the first or last. Quote Link to comment Share on other sites More sharing options...
effigy Posted January 21, 2008 Share Posted January 21, 2008 TMTOWTDI: <pre> <?php $data = 'PD32TR/PD54YT/PD43QW/512RE/51QW/PD43WQ'; echo preg_replace('%(?<=\A|/)(?!PD)%', 'PD', $data); ?> </pre> Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted January 21, 2008 Share Posted January 21, 2008 TMTOWTDI? Quote Link to comment Share on other sites More sharing options...
corbin Posted January 21, 2008 Share Posted January 21, 2008 Hrmmm... I hadn't ever seen that either.... First Google result: TMTOWTDI: /tim�toh'�dee/, abbrev. There's More Than One Way To Do It. This abbreviation of the official motto of Perl is frequently used on newsgroups and mailing lists related to that language. Quote Link to comment Share on other sites More sharing options...
resago Posted January 21, 2008 Share Posted January 21, 2008 $string=preg_replace('/\/(/d/d..)\//','/PD$1/',$string); That will result in an E_WARNING. I see what you're trying to do though, but it won't work if the item is the first or last. oh, yup you're right. Quote Link to comment 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.