Forum Moderators: coopster

Message Too Old, No Replies

str_replace - Can it be shortened?

... must be a way ...

         

internetheaven

8:43 pm on Nov 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm trying to reduce my code on a data feed php scripting program. One of the areas I've found best to start was the str_replace section which I had made quite huge. I've managed to knock off around 10000 bytes so far but I was wondering if there was a way to merge similar replace commands. e.g:

$a[example] = str_replace(" for ", ' ', $a[example]);
$a[example] = str_replace(" of ", ' ', $a[example]);
$a[example] = str_replace(" to ", ' ', $a[example]);
$a[example] = str_replace(" in ", ' ', $a[example]);
$a[example] = str_replace(" at ", ' ', $a[example]);

to something like:

$a[example] = str_replace(" for ", " of ", " to ", " in ", " at ", ' ', $a[example]);

Anyone help?

internetheaven

9:52 pm on Nov 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just as a note on the same lines to avoid opening a second thread - someone suggested:

; Transparent output compression using the zlib library
; Valid values for this option are 'off', 'on', or a specific buffer size ; to be used for compression (default is 4KB) zlib.output_compression = Off

Any pros cons of using this function that I should be aware of?

ergophobe

10:42 pm on Nov 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



str_replace can take arrays for any and all arguments.

$my_array = array(" for", " of", " to");

$new_string = str_replace($my_array, '', $orginal_string);

I don't know whether or not it will be any faster in terms of execution time, but it should allow you to make your code easier to read.

Tom

mincklerstraat

2:16 am on Nov 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



ergophobe's idea sounds best to me in terms of simplicity, just getting the job in a good, transparent way; it's also pretty darn fast since
str_replace()
in general is faster than
preg_replace()
. However, it could be that a line of
preg_replace
might be faster than arrays with
str_replace()
. So if you're a real speed demon, you can try:

$a['example'] = preg_replace_all('#"\s(forŠofŠtoŠinŠat)\s"#', ' ', $a['example']);

and see if it makes any difference.

What you would want to do for speed is put single quotes around your strings in your variable keys -


$a['example']
instead of
$a[example]
- the latter causes a tiny little error (warning or notice, don't remember which) which takes its own wee bit of time.

About zlib compression: in most cases it's a great blessing, it saves you a lot of bandwidth and turns your pages out a lot faster. However, it can be a nuisance since it's a bit heavier for your processor. If your load average is already on the high end and your processor is getting close to moaning and complaining, then it's probably better to leave it off. Usually though the hit on your processor gets a significant compensation since, when your pages are spat out nice and quick, all those processes are freed up and ready to be used again.

You can also consider using a caching system like jpcache (PEAR cache-lite might do something similar, till now I've only tried jpcache). This takes your pages, saves them in pre-gzipped format in a directory, and just returns the cached, already-produced pages - already gzipped, too. Not very useful, though, if your content is really dynamic and changing every few seconds. For user agents that can't handle g-zipped content it just churns out your pages the 'normal' way. Incredibly easy to use, too.

internetheaven

3:48 pm on Nov 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What you would want to do for speed is put single quotes around your strings in your variable keys -

$a['example'] instead of $a[example] - the latter causes a tiny little error (warning or notice, don't remember which) which takes its own wee bit of time.

Thanks for your advice and sorry to sound sceptical, but it was a PHP "expert" that told me to do it WITHOUT quotes. Would the general consensus on this board be that mincklerstraat is correct that without quotes will make the process slower?

And would everyone also agree that:

$a['example'] = preg_replace_all('#"\s(forŠofŠtoŠinŠat)\s"#', ' ', $a['example']);

would be faster than:

$my_array = array(" for", " of", " to");

$new_string = str_replace($my_array, '', $orginal_string);

?

Thanks everyone for your help so far.

internetheaven

4:21 pm on Nov 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I couldn't get the preg_replace to work, it returned:

Fatal error: Call to undefined function: preg_replace_all()

The code was this:

$a['example'] = preg_replace_all('#"\s(The Š For Š Of Š To Š In Š At Š On Š As Š A Š by Š the Š for Š of Š to Š in Š at Š on Š as Š a Š by Š - Š & Š-)\s"#', ' ', $a['example']);

Is there something wrong with that code?

Bonusbana

4:29 pm on Nov 8, 2004 (gmt 0)

10+ Year Member



There is no php command named preg_replace_all as I know of.

Try:

$a['example'] = [blue]preg_replace[/blue]('#"\s(forŠofŠtoŠinŠat)\s"#', ' ', $a['example']);

Bonusbana

4:34 pm on Nov 8, 2004 (gmt 0)

10+ Year Member



...also regarding the $a['example'] issue, I prefer to use simple arrays as objects: $a->example. Two chars less...

internetheaven

4:46 pm on Nov 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for all your advice but none of those are working for some reason:

$a[example] = preg_replace('#"\s(forŠofŠtoŠinŠatŠand)\s"#', ' ', $a[example]);

did not remove the words, neither did:

$a[example] = preg_replace('#"\s( for Š of Š to Š in Š at Š and )\s"#', ' ', $a[example]);

did nothing and:

$stopword_array = array(" for ", " of ", " to ", " in ", " at ", " on ", " as ", " a ", " by ", " the ", " - ", " For ", " Of ", " To ", " In ", " At ", " On ", " As ", " A ", " By ", " The ");
$a[example2] = str_replace($stopword_array, ' ', $a[example1]);

did nothing too. As far as I can see I've followed your instructions and did all three using both $a['example'] and $a[example] to make sure so why are they not working?

Bonusbana

5:14 pm on Nov 8, 2004 (gmt 0)

10+ Year Member



Try this:

$a['example'] = preg_replace('/\s+(forŠofŠtoŠinŠatŠand)\s+/', ' ', $a['example']t);

Works for me.

You also might want to add more stuff in the regExp, like if the word is before a period, comma, parantes, etc...

mincklerstraat

5:22 pm on Nov 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



erhm, sorry for the
preg_replace_all()
- indeed, meant
preg_replace()
. What with
preg_match()
and
preg_match_all()
, and probably not enough caffeine.

internetheaven

6:53 pm on Nov 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Again, thanks for the help but it is still not working, I added this direct code:

$a['example'] = preg_replace('/\s+(forŠofŠtoŠinŠatŠand)\s+/', ' ', $a['example']t);

and it returned a parse error. I had a thought that maybe the surrounding coding was affecting it:

$a[example1] = str_replace(",", '', $a[example1]);
$a["example1"] .= " ";
$a[example2] = str_replace("e-", 'eŁ', $a[example1]);
$a['example2'] = preg_replace('/\s+(forŠofŠtoŠinŠatŠand)\s+/', ' ', $a['example2']t);
$a[example2] = str_replace("xed ", 'x++ ', $a[example2]);

mincklerstraat

7:32 pm on Nov 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry for these problems, internetheaven. For one thing, looking back on my posting, I see I have double quotes there - no idea what I was thinking.

Second of all, when you see 'pipes' here at Webmasterworld (Š), they're always broken pipes - somehow the forum script replaces these. What you need to do is replace every broken pipe with one that doesn't have that gap in the middle. This could also have been causing your problems. Anyways, I went so far as to actually try this code since you've been through such a ringaround and this works:

$a['example'] = 'the thing of the dingaling goes in circles to London gotcha!'; 
$a['example'] = preg_replace('#\s+(forŠofŠtoŠinŠat)\s+#', ' ', $a['example']);
echo $a['example'];

remember to replace those pipes (Š)!

Happy coding.

Bonusbana

11:27 am on Nov 9, 2004 (gmt 0)

10+ Year Member



Also, remove my "t" in:

$a['example'] = preg_replace('/\s+(forŠofŠtoŠinŠatŠand)\s+/', ' ', $a['example']t);

internetheaven

8:13 pm on Nov 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Got it working now, that's alot for your help. I can't say I can see a huge change in the speed but certainly I've knocked out a nice chunk of coding and now know how to keep some of the coding size down in the future.

internetheaven

8:08 pm on Nov 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've just noticed that if a string has two or more of the words included in it ("of" and "the" for example) then it will only remove one of them using the preg_replace function.

Is that how it is supposed to work?

mincklerstraat

10:26 am on Nov 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



My regex and Bonusbana's are basically the same - both match space, one of your words, then space, and replaces the lot with space; however, if you have two words right after each other, I guess the last space of the first word doesn't get counted as the first space of the second word.

This appears to work:


$a['example'] = 'three for abacus to in cactus crashing at Peter and for virtue';
$a['example'] = preg_replace('#\s+(forŠofŠtoŠinŠat)\s+(forŠofŠtoŠinŠat)?[\s]*#', ' ', $a['example']);
echo $a['example'];

output:
three abacus cactus crashing Peter and virtue

If you later get hitched up with punctuation before or after the word, you can try replacing all occurrences of \s in the regex with \W - we didn't do that in the beginning since you asked for spaces (and probably weren't really thinking that far ahead).

Note: this perhaps a rather klugey way of getting this task done, a real regex whiz might have a more elegant way of solving this problem.

baertyp

10:49 am on Nov 11, 2004 (gmt 0)

10+ Year Member



Internetheaven,

to address your question on whether to reference array with or without quotes, see

[de3.php.net ]

and scroll down to the section "Array do's and don'ts".

There you'll find a rather exhaustive explanation on why NOT quoting is bad coding style and can cause problems in the future.

Just my $0.02...

Regards
Markus