Forum Moderators: coopster
$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?
; 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?
str_replace()in general is faster than
preg_replace(). However, it could be that a line of
preg_replacemight 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']);
What you would want to do for speed is put single quotes around your strings in your variable keys -
instead of
$a['example']
$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.
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.
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?
$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?
$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]);
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.
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.
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