Page 1 of 2

newbie str_replace question

Posted: Wed Sep 16, 2009 9:23 am
by tabutcher
Briefly what I want my code to do:
Read in the text file
Explode each line of text into 3 different arrays
For example one line of the text file would be 'Name Company Price'
I'd like to store the price into an array as a number but it would store the $ symbol as well

I tried using the str_replace but to no avail.
What my thinking process is to remove the $ symbol before storing it in the array
At the moment the return value is 0
Thanks in advance for any help

Code: Select all

 
while(!feof($textFile)){
    $strRecord = fgets($textFile);
    $arField = explode('=', $strRecord);
    $arName[$intCounter] = $arField[0];
    $arCompany[$intCounter] = $arField[1];
    str_replace('$',"",$arField[2]);
    $arPrice[$intCounter] = (int)$arField[2];
    
    echo "$arProducer[$intCounter]<br />";
    echo "$arName[$intCounter]<br />";
    echo "$arPrice[$intCounter]<br />";
    $intCounter ++;
}
 

Re: newbie str_replace question

Posted: Wed Sep 16, 2009 9:38 am
by ricehigh
You could try either of the following (untested):

Code: Select all

$arPrice[$intCounter] = (int) str_replace('$',"",$arField[2]);
or:

Code: Select all

$arField[2] = str_replace('$',"",$arField[2]);
$arPrice[$intCounter] = $arField[2]

Re: newbie str_replace question

Posted: Wed Sep 16, 2009 9:41 am
by Weiry
if you want to replace '$' signs in your string, you would be better off using preg_replace. (imo)

Code: Select all

$text1 = '$99,Test,512';
$text2 = preg_replace('/\$/','',$text1);
$arr = explode(',',$text2);
print_r($arr);
so you could modify your code to:

Code: Select all

$strRecord = fgets($textFile);
$replacedText= preg_replace('/\$/','',$strRecord );
$arField = explode('=', $replacedText);
Or similar to at least :)

I did try with str_replace, and couldnt get it working, but i like preg_replace better anyways ^^

Re: newbie str_replace question

Posted: Wed Sep 16, 2009 9:55 am
by ricehigh
Actually, if you just want to remove dollar-signs, you would not be better off using preg_replace.
str_replace performs much better than preg_replace, because it's more simple. So in any task, where preg_replace does the same job as str_replace, you should most definitly use str_replace.

(Weiry: you should only use regular expressions, where regular expressions are needed, because they are rather slow)

Re: newbie str_replace question

Posted: Wed Sep 16, 2009 10:39 am
by jackpf
Or...you could try this:

Code: Select all

$str = substr($str, 1, strlen($str));
I wonder how that compares against str_replace()... :D

Re: newbie str_replace question

Posted: Wed Sep 16, 2009 10:50 am
by ricehigh
jackpf: Nicely done :) According to my test of the current scenario substr() performs about 20% faster than str_replace().

My test is based on 10 million iterations and the string "$100".

Re: newbie str_replace question

Posted: Wed Sep 16, 2009 10:57 am
by jackpf
Awesome

:P

Re: newbie str_replace question

Posted: Wed Sep 16, 2009 11:05 am
by ricehigh
... Oh and just for comparison: preg_replace is about 240% slower than substr().

Here are the test results for 10 million iterations of the string "$100" converting it to "100":
substr(): 14.16 s
str_replace(): 17.44 s
pre_replace(): 47.49 s

Re: newbie str_replace question

Posted: Wed Sep 16, 2009 12:01 pm
by jackpf
Yeah...preg_replace() will be way slower since it has to go through the regex engine or whatever it's called.

But yeah, I win :P

Lol, nice work ricehigh :)

Re: newbie str_replace question

Posted: Wed Sep 16, 2009 12:18 pm
by Mirge
ricehigh wrote:... Oh and just for comparison: preg_replace is about 240% slower than substr().

Here are the test results for 10 million iterations of the string "$100" converting it to "100":
substr(): 14.16 s
str_replace(): 17.44 s
pre_replace(): 47.49 s
Strange... with 10 million iterations, I get:

substr(): 18.333s
str_replace(): 16.365s

Code used:

Code: Select all

 
<?php
 
$amount = '$100';
$iterations = 10000000;
 
for($i = 1; $i <= $iterations; $i++) {
#       substr($amount, 1, strlen($amount));
#       str_replace('$', '', $amount);
}
 
?>
 
Uncommented one at a time of course.

Re: newbie str_replace question

Posted: Wed Sep 16, 2009 12:24 pm
by superdezign
Why are we all using strlen() with substr() again?

Re: newbie str_replace question

Posted: Wed Sep 16, 2009 12:29 pm
by Mirge
superdezign wrote:Why are we all using strlen() with substr() again?
Because I was just timing what was already posted.

Without specifying strlen($amount)... I get: 12.649s

Pretty dramatic improvement I think... again with specifying strlen($amount) I get: 18.153s

EDIT:
Using: ltrim($amount, '$'); -- I get: 14.836s, just out of curiosity :).

Re: newbie str_replace question

Posted: Wed Sep 16, 2009 12:32 pm
by ricehigh
superdezign: We are using strlen(), to specify how much of the string we want to substract, that is from the 1st character (remember in programming counting starts from zero), to the total length of the string. Se the php documentation for more info: http://dk.php.net/manual/en/function.substr.php

Edit: actually it seems to perform better without the $length parameter. I honestly wasn't aware of this.

Mirge:
Code i used:
substr()

Code: Select all

<?php
$time = microtime(true);
 
for($x=0;$x<10000000;$x++){
    $str = "$100";
    $str = substr($str, 1, strlen($str));
}
echo $str;
$time = (microtime(true)-$time);
printf('<br/>Page generated in: %0.2f s<br/>',$time);
?>
str_replace():

Code: Select all

<?php
$time = microtime(true);
 
for($x=0;$x<10000000;$x++){
    $str = "$100";
    $str = str_replace('$',"",$str);
}
echo $str;
$time = (microtime(true)-$time);
printf('<br/>Page generated in: %0.2f s<br/>',$time);
?>
In your code you seem to neglect to redefine your string, making substr substract from an empty string.
Furthermore when you neglect to redefine your string you're not testing performance of removing the dollar sign from the string, since you're only removing it at the first iteration.

Re: newbie str_replace question

Posted: Wed Sep 16, 2009 12:33 pm
by superdezign
Mirge wrote:Without specifying strlen($amount)... I get: 12.649s

Pretty dramatic improvement I think... again with specifying strlen($amount) I get: 18.153s
I'd assume so. :3
I was just stating that we were making an unfair comparison.

Re: newbie str_replace question

Posted: Wed Sep 16, 2009 12:35 pm
by superdezign
ricehigh wrote:superdezign: We are using strlen(), to specify how much of the string we want to substract, that is from the 1st character (remember in programming counting starts from zero), to the total length of the string. Se the php documentation for more info: http://dk.php.net/manual/en/function.substr.php
You posted the link, but did you read it?
substr() doesn't need a length in order to get the entire rest of the string.

EDIT: Missed your edit. Sry.