Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #10715 > unrolled thread

str_replace & assign to a var?

Started byTwayne <nobody@spamcop.net>
First post2013-09-04 13:29 -0400
Last post2013-09-05 10:34 -0400
Articles 6 — 3 participants

Back to article view | Back to comp.lang.php


Contents

  str_replace & assign to a var? Twayne <nobody@spamcop.net> - 2013-09-04 13:29 -0400
    Re: str_replace & assign to a var? NEVER MIND Twayne <nobody@spamcop.net> - 2013-09-04 14:00 -0400
    Re: str_replace & assign to a var? "J.O. Aho" <user@example.net> - 2013-09-04 20:16 +0200
      Re: str_replace & assign to a var? Twayne <nobody@spamcop.net> - 2013-09-04 14:52 -0400
      Re: str_replace & assign to a var? Curtis Dyer <dyer85@gmail.com> - 2013-09-05 08:48 +0000
        Re: str_replace & assign to a var? Twayne <nobody@spamcop.net> - 2013-09-05 10:34 -0400

#10715 — str_replace & assign to a var?

FromTwayne <nobody@spamcop.net>
Date2013-09-04 13:29 -0400
Subjectstr_replace & assign to a var?
Message-ID<l07qmq$8o0$1@speranza.aioe.org>
Hi Guys & Gals,

If this is a dumb question I apologize but I'm stuck. Yes, I'm 
essentially a newbie to many things yet in PHP.

I have the following canned code:
------------
$arr = array("blue","red","green","yellow");
print_r(str_replace("red","pink",$arr,$i));
echo "<br>" . "Replacements: $i";
-----------

All I want to do is change "red" to "pink" IN the array or a new array. 
How do I do that?


In other words, I want to change the original array to have "pink" in it 
instead of "red".

My actual goal is to take a var of "-1111-111-111" to "1111111111" and 
remove the dashes. The appearance of the dashes is essentially random 
but there will only be 3 of them max or even none.

Caveat: the dashes can occur almost anywhere; the var could be, for 
example, -1-1-1" , 111, 999, 99-9, 9-9, etc. on up to all 9's. and so on.

I can count the dashes, see the dashes in one string and all that, but I 
cannot modify seem to remove the dashes from the orginal var in order to 
create a var without any dashes.
    If I can get the digits-only into a new var then I can perform the 
analysis I wish to on those documents.
    I'd also like to avoid using regex.

TIA,

Twayne`

[toc] | [next] | [standalone]


#10717 — Re: str_replace & assign to a var? NEVER MIND

FromTwayne <nobody@spamcop.net>
Date2013-09-04 14:00 -0400
SubjectRe: str_replace & assign to a var? NEVER MIND
Message-ID<l07sgh$dra$1@speranza.aioe.org>
In reply to#10715
On 2013-09-04 1:29 PM, Twayne wrote:
> Hi Guys & Gals,
>
> If this is a dumb question I apologize but I'm stuck. Yes, I'm
> essentially a newbie to many things yet in PHP.
>
> I have the following canned code:
> ------------
> $arr = array("blue","red","green","yellow");
> print_r(str_replace("red","pink",$arr,$i));
> echo "<br>" . "Replacements: $i";
> -----------
>
I got it; no idea why it wouldn't work last night, but ... this works fine:
-----------------
$acode = $code;
echo "<br />";
  $acode = str_replace("-", "", $code)."<br />" ;
  echo $acode." <br />";
  echo "acode is : " . strlen($acode);
------- OUTPUT ---------
Original Code : 3622508-980
Length : 11 characters.
3622508980

acode is : 16
----------------------

Sorry for the false start;
Momentary brain fart I guess, but really irritating anyway!

Twayne`


> All I want to do is change "red" to "pink" IN the array or a new array.
> How do I do that?
>
>
> In other words, I want to change the original array to have "pink" in it
> instead of "red".
>
> My actual goal is to take a var of "-1111-111-111" to "1111111111" and
> remove the dashes. The appearance of the dashes is essentially random
> but there will only be 3 of them max or even none.
>
> Caveat: the dashes can occur almost anywhere; the var could be, for
> example, -1-1-1" , 111, 999, 99-9, 9-9, etc. on up to all 9's. and so on.
>
> I can count the dashes, see the dashes in one string and all that, but I
> cannot modify seem to remove the dashes from the orginal var in order to
> create a var without any dashes.
>     If I can get the digits-only into a new var then I can perform the
> analysis I wish to on those documents.
>     I'd also like to avoid using regex.
>
> TIA,
>
> Twayne`
>
>

[toc] | [prev] | [next] | [standalone]


#10718

From"J.O. Aho" <user@example.net>
Date2013-09-04 20:16 +0200
Message-ID<b8pbn7Fdg6bU1@mid.individual.net>
In reply to#10715
On 04/09/13 19:29, Twayne wrote:
> Hi Guys & Gals,
>
> If this is a dumb question I apologize but I'm stuck. Yes, I'm
> essentially a newbie to many things yet in PHP.
>
> I have the following canned code:
> ------------
> $arr = array("blue","red","green","yellow");
> print_r(str_replace("red","pink",$arr,$i));
> echo "<br>" . "Replacements: $i";
> -----------
>
> All I want to do is change "red" to "pink" IN the array or a new array.
> How do I do that?
>
>
> In other words, I want to change the original array to have "pink" in it
> instead of "red".
>
> My actual goal is to take a var of "-1111-111-111" to "1111111111" and
> remove the dashes. The appearance of the dashes is essentially random
> but there will only be 3 of them max or even none.
>

Here is an example how to make it neatly and this can be made quite 
dynamic by chancing the function to use for remapping the cell content:

<?php

function filterNumbers($str) {
         return str_replace('-', '', $str);
}

$oldArray=array('1234-1234', '2345-2345', 'ABCD.FGHI', '3456.3456', 
'-0987-0987');

$newArray=array_map('filterNumbers', $oldArray);

var_dump($newArray);

?>

The filterNumbers do the actual work (you can make it a lot more 
advanced, depending on your needs).

then you input the remapping function and the original array as 
arguments and you then get a modified array back.

If you want to remove items in an array, then use the array_filter(), 
the argument order is different and the function needs to return true or 
false.

-- 

  //Aho

[toc] | [prev] | [next] | [standalone]


#10719

FromTwayne <nobody@spamcop.net>
Date2013-09-04 14:52 -0400
Message-ID<l07vh7$m66$1@speranza.aioe.org>
In reply to#10718
On 2013-09-04 2:16 PM, J.O. Aho wrote:
> On 04/09/13 19:29, Twayne wrote:
>> Hi Guys & Gals,
>>
...
>>
>> My actual goal is to take a var of "-1111-111-111" to "1111111111" and
>> remove the dashes. The appearance of the dashes is essentially random
>> but there will only be 3 of them max or even none.
>>
>
> Here is an example how to make it neatly and this can be made quite
> dynamic by chancing the function to use for remapping the cell content:
>
> <?php
>
> function filterNumbers($str) {
>          return str_replace('-', '', $str);
> }
>
> $oldArray=array('1234-1234', '2345-2345', 'ABCD.FGHI', '3456.3456',
> '-0987-0987');
>
> $newArray=array_map('filterNumbers', $oldArray);
>
> var_dump($newArray);
>
> ?>
>
> The filterNumbers do the actual work (you can make it a lot more
> advanced, depending on your needs).
>
> then you input the remapping function and the original array as
> arguments and you then get a modified array back.
>
> If you want to remove items in an array, then use the array_filter(),
> the argument order is different and the function needs to return true or
> false.
>

I'm not ignoring your advice; just thinking & doing some research; those 
are new areas for me a and they look very useful.  THANKS!

Twayne`

[toc] | [prev] | [next] | [standalone]


#10720

FromCurtis Dyer <dyer85@gmail.com>
Date2013-09-05 08:48 +0000
Message-ID<l09ggg$30v$1@dont-email.me>
In reply to#10718
"J.O. Aho" <user@example.net> wrote:

> On 04/09/13 19:29, Twayne wrote:
>> Hi Guys & Gals,
>>
>> If this is a dumb question I apologize but I'm stuck. Yes, I'm
>> essentially a newbie to many things yet in PHP.
>>
>> I have the following canned code:
>> ------------
>> $arr = array("blue","red","green","yellow");
>> print_r(str_replace("red","pink",$arr,$i));
>> echo "<br>" . "Replacements: $i";
>> -----------
>>
>> All I want to do is change "red" to "pink" IN the array or a
>> new array. How do I do that?
>>
>>
>> In other words, I want to change the original array to have
>> "pink" in it instead of "red".

Have you checked the PHP documentation? This is clearly explained: 
<http://php.net/str_replace>.

<snip>

> Here is an example how to make it neatly and this can be made
> quite dynamic by chancing the function to use for remapping the
> cell content: 
> 
> <?php
> 
> function filterNumbers($str) {
>          return str_replace('-', '', $str);
> }
> 
> $oldArray=array('1234-1234', '2345-2345', 'ABCD.FGHI',
> '3456.3456', '-0987-0987');
> 
> $newArray=array_map('filterNumbers', $oldArray);
> 
> var_dump($newArray);
> 
> ?>
> 
> The filterNumbers do the actual work (you can make it a lot more
> advanced, depending on your needs).

In this case, however, it seems unnecessary. The `str_replace' 
function will already return the altered array when the `subject' 
parameter is an array. So, in the OP's code snippet above, we need 
to change the `str_replace' line:

  <?php
  $i = 0;

  /* ... */

  $arr = str_replace('red', 'pink', $arr, $i);
  print_r($arr);
  ?>


<snip>

-- 
Curtis Dyer
<?$x='<?$x=%c%s%c;printf($x,39,$x,39);?>';printf($x,39,$x,39);?>

[toc] | [prev] | [next] | [standalone]


#10721

FromTwayne <nobody@spamcop.net>
Date2013-09-05 10:34 -0400
Message-ID<l0a4p3$2iv$1@speranza.aioe.org>
In reply to#10720
On 2013-09-05 4:48 AM, Curtis Dyer wrote:
> "J.O. Aho" <user@example.net> wrote:
>
>> On 04/09/13 19:29, Twayne wrote:
>>> Hi Guys & Gals,
>>>
...
>>> I have the following canned code:
>>> ------------
>>> $arr = array("blue","red","green","yellow");
>>> print_r(str_replace("red","pink",$arr,$i));
>>> echo "<br>" . "Replacements: $i";
>>> -----------
>>>
>>> All I want to do is change "red" to "pink" IN the array or a
>>> new array. How do I do that?
...

>>
>> The filterNumbers do the actual work (you can make it a lot more
>> advanced, depending on your needs).
>
> In this case, however, it seems unnecessary. The `str_replace'
> function will already return the altered array when the `subject'
> parameter is an array. So, in the OP's code snippet above, we need
> to change the `str_replace' line:
>
>    <?php
>    $i = 0;
>
>    /* ... */
>
>    $arr = str_replace('red', 'pink', $arr, $i);
>    print_r($arr);
>    ?>
>
>
> <snip>
>

Hmm, I've read the Manual, w3schools, Tizag and several others but never 
did pick up on 'subject' being my issue.

Thanks much,

Twayne`

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web