johnny44 Posted March 23, 2008 Share Posted March 23, 2008 I want to replace all plain text apostrophe's ( ' ) in a certain string with the html variant ( ’ ). The apostrophes invariably come before letters like s, t and d. This code works fine: $blah = str_replace( "'s" , "’s" , $blah ); $blah = str_replace( "'t" , "’t" , $blah ); $blah = str_replace( "'d" , "’d" , $blah ); ... but I trying to learn preg_replace. Nothing I have tried works. The closest I got is this: $blah = preg_replace( "/'[a-z]/" , "’$0" , $blah ); but this just adds the html apostrophe without removing the plain one. Can anyone show me what would work? Quote Link to comment Share on other sites More sharing options...
Orio Posted March 23, 2008 Share Posted March 23, 2008 $blah = preg_replace( "/'([a-z])/" , "’$1" , $blah ); Orio. Quote Link to comment Share on other sites More sharing options...
johnny44 Posted March 24, 2008 Author Share Posted March 24, 2008 Gosh, thanks Orio. Much appreciated !! Quote Link to comment Share on other sites More sharing options...
effigy Posted March 24, 2008 Share Posted March 24, 2008 Or avoid the capture altogether: $blah = preg_replace("/'(?=[a-z])/", '’', $blah ); Quote Link to comment Share on other sites More sharing options...
johnny44 Posted March 25, 2008 Author Share Posted March 25, 2008 Avoid the capture. Good, good, good. Thanks Effigy. 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.