Jump to content

[SOLVED] Preg Replace at start and end string


sastro

Recommended Posts

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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..

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.