sastro Posted July 4, 2009 Share Posted July 4, 2009 The string is "...multiplayer online games (MMOGs). Virtual currencies are digitized forms of money issued by... stripes gravitate towards online money laundering. strategies..." How to replace "..." at begining and end of the string? Thanks in advance Quote Link to comment Share on other sites More sharing options...
.josh Posted July 4, 2009 Share Posted July 4, 2009 replace it with what? Quote Link to comment Share on other sites More sharing options...
sastro Posted July 4, 2009 Author Share Posted July 4, 2009 with empty Quote Link to comment Share on other sites More sharing options...
.josh Posted July 4, 2009 Share Posted July 4, 2009 okay well if you're just wanting to remove 3 dots from the beginning and end of a string, you can do this: $string = substr($string,3,-3); or this: $string = trim($string,'.'); Quote Link to comment Share on other sites More sharing options...
sastro Posted July 4, 2009 Author Share Posted July 4, 2009 I need preg_replace, because sometimes, the string not contain "..." Thanks Quote Link to comment Share on other sites More sharing options...
.josh Posted July 4, 2009 Share Posted July 4, 2009 You said in your OP that you wanted to replace "..." with nothing. Now you're saying that "..." will change. Okay, so here's the deal: I'm not psychic, nor is anybody else around here. Rest assured, we are trying our hardest to fix that issue, but until then, if you want help, you are going to have to specifically mention what it is you're trying to do here. What will be at the beginning and end? Quote Link to comment Share on other sites More sharing options...
sastro Posted July 4, 2009 Author Share Posted July 4, 2009 I'm sory for my bad english. Here is my code : <?php $a=array( '...multiplayer online games (MMOGs). Virtual currencies are digitized forms ... stripes gravitate towards online money laundering. strategies...', 'Google Bugs May Contribute to Lower AdSense Income... AdSense publishers can now adjust the sizes of fonts within the ad units...', '...revenues, Carter set out to fine-tune his AdSense program to maximize his cash flow.' ); foreach($a AS $v){ echo preg_replace('[^\.{3}$]','',$v).'<br>'; } ?> But the result is ...multiplayer online games (MMOGs). Virtual currencies are digitized forms ... stripes gravitate towards online money laundering. strategies... Google Bugs May Contribute to Lower AdSense Income... AdSense publishers can now adjust the sizes of fonts within the ad units... ...revenues, Carter set out to fine-tune his AdSense program to maximize his cash flow. I need the result just like below multiplayer online games (MMOGs). Virtual currencies are digitized forms ... stripes gravitate towards online money laundering. strategies Google Bugs May Contribute to Lower AdSense Income... AdSense publishers can now adjust the sizes of fonts within the ad units revenues, Carter set out to fine-tune his AdSense program to maximize his cash flow. I hope you understand what i mean.. Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted July 4, 2009 Share Posted July 4, 2009 One solution could be: $a=array( '...multiplayer online games (MMOGs). Virtual currencies are digitized forms ... stripes gravitate towards online money laundering. strategies...', 'Google Bugs May Contribute to Lower AdSense Income... AdSense publishers can now adjust the sizes of fonts within the ad units...', '...revenues, Carter set out to fine-tune his AdSense program to maximize his cash flow.' ); foreach($a as &$val){ if(substr($val, 0, 3) == '...'){ $val = substr($val, 3); } if(substr($val, -3, 3) == '...'){ $val = substr($val, 0, -3); } } echo '<pre>'.print_r($a, true); Quote Link to comment Share on other sites More sharing options...
.josh Posted July 4, 2009 Share Posted July 4, 2009 Okay so you are saying that sometimes there will be "..." at the beginning and end, sometimes not, and the goal is to remove them if they are there? Use the trim() Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted July 4, 2009 Share Posted July 4, 2009 I don't think trim will work in this case, because it will wipe out all periods, not just beginning / ending instances of '...' (unless I'm missing something here) Quote Link to comment Share on other sites More sharing options...
.josh Posted July 4, 2009 Share Posted July 4, 2009 trim only removes stuff on the beginning and end of a string, not stuff in-between. Though you did bring up a good point: It will remove all dots from the end, so if It's a regular sentence with a single dot, that will be removed...but considering (according to his before -> after examples) he seems to be fine with some results having no periods, if he can live will all results being without a period on the end, this would be ideal. Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted July 4, 2009 Share Posted July 4, 2009 Sorry, that's what I meant.. all periods (initial or ending). Yeah.. I think it would be cool if PHP intregrated a version of trim with perhaps a flag or something that allowed individual characters (as it currently does) or a forced sequence of characters to be removed from beginning / ending). Quote Link to comment Share on other sites More sharing options...
sastro Posted July 4, 2009 Author Share Posted July 4, 2009 found the solution <?php $a=array( '...multiplayer online games (MMOGs). Virtual currencies are digitized forms ... stripes gravitate towards online money laundering. strategies...', 'Google Bugs May Contribute to Lower AdSense Income... AdSense publishers can now adjust the sizes of fonts within the ad units...', '...revenues, Carter set out to fine-tune his AdSense program to maximize his cash flow.' ); foreach($a AS $v){ echo preg_replace('[^\.{3}|\.{3}$]','',$v).'<br>'; } ?> Thanks anyway for help Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted July 4, 2009 Share Posted July 4, 2009 found the solution You found a solution (yes, you can do it that was as well as what I have done). Granted, understand that you are making a replacement to a local copy of the array. You are not making a change to the array itself. 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.