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 Link to comment https://forums.phpfreaks.com/topic/87060-solved-adding-text/ 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); Link to comment https://forums.phpfreaks.com/topic/87060-solved-adding-text/#findComment-445241 Share on other sites More sharing options...
redfox180 Posted January 21, 2008 Author Share Posted January 21, 2008 It doesn't seem to work... Link to comment https://forums.phpfreaks.com/topic/87060-solved-adding-text/#findComment-445250 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 ?> Link to comment https://forums.phpfreaks.com/topic/87060-solved-adding-text/#findComment-445256 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! Link to comment https://forums.phpfreaks.com/topic/87060-solved-adding-text/#findComment-445265 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); Link to comment https://forums.phpfreaks.com/topic/87060-solved-adding-text/#findComment-445272 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. Link to comment https://forums.phpfreaks.com/topic/87060-solved-adding-text/#findComment-445299 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> Link to comment https://forums.phpfreaks.com/topic/87060-solved-adding-text/#findComment-445305 Share on other sites More sharing options...
Daniel0 Posted January 21, 2008 Share Posted January 21, 2008 TMTOWTDI? Link to comment https://forums.phpfreaks.com/topic/87060-solved-adding-text/#findComment-445313 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. Link to comment https://forums.phpfreaks.com/topic/87060-solved-adding-text/#findComment-445316 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. Link to comment https://forums.phpfreaks.com/topic/87060-solved-adding-text/#findComment-445486 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.