newbie str_replace question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

tabutcher
Forum Newbie
Posts: 20
Joined: Fri Aug 21, 2009 7:10 am

newbie str_replace question

Post 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 ++;
}
 
ricehigh
Forum Newbie
Posts: 21
Joined: Mon Sep 14, 2009 5:18 pm

Re: newbie str_replace question

Post 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]
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: newbie str_replace question

Post 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 ^^
ricehigh
Forum Newbie
Posts: 21
Joined: Mon Sep 14, 2009 5:18 pm

Re: newbie str_replace question

Post 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)
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: newbie str_replace question

Post 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
ricehigh
Forum Newbie
Posts: 21
Joined: Mon Sep 14, 2009 5:18 pm

Re: newbie str_replace question

Post 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".
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: newbie str_replace question

Post by jackpf »

Awesome

:P
ricehigh
Forum Newbie
Posts: 21
Joined: Mon Sep 14, 2009 5:18 pm

Re: newbie str_replace question

Post 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
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: newbie str_replace question

Post 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 :)
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: newbie str_replace question

Post 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.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: newbie str_replace question

Post by superdezign »

Why are we all using strlen() with substr() again?
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: newbie str_replace question

Post 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 :).
Last edited by Mirge on Wed Sep 16, 2009 12:33 pm, edited 1 time in total.
ricehigh
Forum Newbie
Posts: 21
Joined: Mon Sep 14, 2009 5:18 pm

Re: newbie str_replace question

Post 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.
Last edited by ricehigh on Wed Sep 16, 2009 12:34 pm, edited 1 time in total.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: newbie str_replace question

Post 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.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: newbie str_replace question

Post 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.
Last edited by superdezign on Wed Sep 16, 2009 12:37 pm, edited 1 time in total.
Post Reply