justinh Posted February 3, 2009 Share Posted February 3, 2009 $unitbold = preg_replace('/^[uu][Nn][ii][Tt]/', "<b>Unit Information</b>", $objComport->ReadString()); What if i wanted to replace multiple words in a string? For instance, this takes the word UNIT ( or any other capitalization of it ) and makes it bold. What if I wanted to find UNIT, make that bold, Find QTY, make that bold, and find AMOUNT, and make that bold? Can you do it all in one preg_replace? Thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/143550-preg-replace-question/ Share on other sites More sharing options...
gevans Posted February 3, 2009 Share Posted February 3, 2009 If you know the exact string why not use str_ireplace(). It's a case incensitive string replace that accepts arrays for the needle and replacing fields Quote Link to comment https://forums.phpfreaks.com/topic/143550-preg-replace-question/#findComment-753111 Share on other sites More sharing options...
justinh Posted February 3, 2009 Author Share Posted February 3, 2009 Well I just got Mastering Regular Expressions 3rd Edition in the mail, So I'm trying to get a grasp on the subject Quote Link to comment https://forums.phpfreaks.com/topic/143550-preg-replace-question/#findComment-753112 Share on other sites More sharing options...
gevans Posted February 3, 2009 Share Posted February 3, 2009 Ahhh right, well if you are going to use this is a live environment I'd used str_ireplace() But for learning why not try... <?php $string = 'The quick brown fox jumped over the lazy dog.'; $patterns[0] = '/quick/'; $patterns[1] = '/brown/'; $patterns[2] = '/fox/'; $replacements[2] = 'bear'; $replacements[1] = 'black'; $replacements[0] = 'slow'; ksort($patterns); ksort($replacements); echo preg_replace($patterns, $replacements, $string); //straight from php.net (hope it makes sense) ?> Quote Link to comment https://forums.phpfreaks.com/topic/143550-preg-replace-question/#findComment-753124 Share on other sites More sharing options...
.josh Posted February 3, 2009 Share Posted February 3, 2009 $array = array('unit','qty','amount'); $string = "unit something qty amount more something unit blah qty"; foreach ($array as $word) { $string = preg_replace("~$word~i","<b>$0</b>",$string); } Quote Link to comment https://forums.phpfreaks.com/topic/143550-preg-replace-question/#findComment-753132 Share on other sites More sharing options...
trq Posted February 3, 2009 Share Posted February 3, 2009 You can simply use | to seperate your matches. eg; /^unit|qty|amount/i Quote Link to comment https://forums.phpfreaks.com/topic/143550-preg-replace-question/#findComment-753134 Share on other sites More sharing options...
.josh Posted February 3, 2009 Share Posted February 3, 2009 btw str_ireplace will be much faster than preg_replace, but you have to have php5+ to use it. You can simply use | to seperate your matches. eg; /^unit|qty|amount/i You could, but alternation is "a lot" slower. Quote Link to comment https://forums.phpfreaks.com/topic/143550-preg-replace-question/#findComment-753136 Share on other sites More sharing options...
justinh Posted February 3, 2009 Author Share Posted February 3, 2009 <?php $search = array("UNIT", "QTY", "AMOUNT"); $replace = array("<tr><td><b>UNIT</b></td>", "<td><b>QTY</b></td>", "<td><b>AMOUNT</b></td></tr>"); $string = "<table>UNIT QTY AMOUNT</table>"; $tablestring = str_replace($search, $replace, $string); echo $tablestring; ?> A lot faster. Quote Link to comment https://forums.phpfreaks.com/topic/143550-preg-replace-question/#findComment-753163 Share on other sites More sharing options...
.josh Posted February 3, 2009 Share Posted February 3, 2009 except that that isn't case insensitive like you specified. Quote Link to comment https://forums.phpfreaks.com/topic/143550-preg-replace-question/#findComment-753164 Share on other sites More sharing options...
justinh Posted February 3, 2009 Author Share Posted February 3, 2009 Gotcha, str_ireplace is for searching and replacing case insensitive data within a string. Quote Link to comment https://forums.phpfreaks.com/topic/143550-preg-replace-question/#findComment-753169 Share on other sites More sharing options...
gevans Posted February 3, 2009 Share Posted February 3, 2009 sorted! Quote Link to comment https://forums.phpfreaks.com/topic/143550-preg-replace-question/#findComment-753171 Share on other sites More sharing options...
justinh Posted February 3, 2009 Author Share Posted February 3, 2009 Okay another quick question, Is putting regex inside a str_replace do-able? Quote Link to comment https://forums.phpfreaks.com/topic/143550-preg-replace-question/#findComment-753172 Share on other sites More sharing options...
gevans Posted February 3, 2009 Share Posted February 3, 2009 nope Quote Link to comment https://forums.phpfreaks.com/topic/143550-preg-replace-question/#findComment-753176 Share on other sites More sharing options...
justinh Posted February 3, 2009 Author Share Posted February 3, 2009 soo.. heres what I have so far.. $search = array("UNIT", "QTY", "AMOUNT", "1c", "10c"); $replace = array("<tr><td><b>UNIT</b></td>", "<td><b>QTY</b></td>", "<td><b>AMOUNT</b></td></tr>", "Pennies", "Dimes"); $string = $objComport->ReadString(); $tablestring = str_replace($search, $replace, $string); echo $tablestring; Heres the output of $tablestring: UNIT QTY AMOUNT Pennies 18 0.18+ Dimes 3 0.30+ 1 1 1.00+ TOTAL 1.48+ I need to get the 1 (under the word "Dimes") to say Dollars. So would I need to throw out the str_replace and go with preg_replace? Not sure how to do this. Thanks for the help, you guys rock Quote Link to comment https://forums.phpfreaks.com/topic/143550-preg-replace-question/#findComment-753183 Share on other sites More sharing options...
gevans Posted February 3, 2009 Share Posted February 3, 2009 Ahhhh. From here it looks like you will need to use a regular expression but unfortunately that's to complicated for my little brain, lets hope on of the other guys can help.... Quote Link to comment https://forums.phpfreaks.com/topic/143550-preg-replace-question/#findComment-753186 Share on other sites More sharing options...
justinh Posted February 3, 2009 Author Share Posted February 3, 2009 Ahhh right, well if you are going to use this is a live environment I'd used str_ireplace() But for learning why not try... <?php $string = 'The quick brown fox jumped over the lazy dog.'; $patterns[0] = '/quick/'; $patterns[1] = '/brown/'; $patterns[2] = '/fox/'; $replacements[2] = 'bear'; $replacements[1] = 'black'; $replacements[0] = 'slow'; ksort($patterns); ksort($replacements); echo preg_replace($patterns, $replacements, $string); //straight from php.net (hope it makes sense) ?> I'm going to play arround with this.. Think this is what I'm looking for. Quote Link to comment https://forums.phpfreaks.com/topic/143550-preg-replace-question/#findComment-753192 Share on other sites More sharing options...
.josh Posted February 3, 2009 Share Posted February 3, 2009 $string = "1 1 1.00+"; $string = preg_replace("~^1~","Dollars",$string); Quote Link to comment https://forums.phpfreaks.com/topic/143550-preg-replace-question/#findComment-753194 Share on other sites More sharing options...
gevans Posted February 3, 2009 Share Posted February 3, 2009 $string = "1 1 1.00+"; $string = preg_replace("~^1~","Dollars",$string); His whole string is something similar to this... UNIT QTY AMOUNT Pennies 18 0.18+ Dimes 3 0.30+ 1 1 1.00+ TOTAL 1.48+ Quote Link to comment https://forums.phpfreaks.com/topic/143550-preg-replace-question/#findComment-753197 Share on other sites More sharing options...
.josh Posted February 3, 2009 Share Posted February 3, 2009 Where is this data coming from? Quote Link to comment https://forums.phpfreaks.com/topic/143550-preg-replace-question/#findComment-753203 Share on other sites More sharing options...
justinh Posted February 3, 2009 Author Share Posted February 3, 2009 A money counting system connected via serial port. Quote Link to comment https://forums.phpfreaks.com/topic/143550-preg-replace-question/#findComment-753205 Share on other sites More sharing options...
.josh Posted February 3, 2009 Share Posted February 3, 2009 $string = "UNIT QTY AMOUNT Pennies 18 0.18+ Dimes 3 0.30+ 1 1 1.00+ TOTAL 1.48+"; $string = preg_replace("~(.+Dimes)([^\+]+?\+\s)(\d+)(.+)~",'$1$2Dollars$4',$string); Quote Link to comment https://forums.phpfreaks.com/topic/143550-preg-replace-question/#findComment-753221 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.