You are here: Re: Improve this algorithm? « All PHP « IT news, forums, messages
Re: Improve this algorithm?

Posted by Curtis on 12/09/05 02:31

Stefan Rybacki <stefan.rybacki@gmx.net> wrote in message
news:3vqh5hF17b2rbU1@individual.net...
> Curtis wrote:
> >...
> > We found the following considerably more efficient,
> > submitting each paragraph in turn to a series like this:
> >
> >
> > if (strstr($text, "/"))
> >
> >
> > $text = preg_replace("/ ETC.
> >
> >
> > if (strstr($text, "*"))
> >
> >
> > $text = preg_replace("/ ETC.
> >
> >
> > if (strstr($text, "^"))
> >
> >
> > $text = preg_replace("/ ETC.
> >
> > This sequence goes on for a goodly number of characters.
> >...
> > Suggestions?
> >
>
>
> Is
> $text = preg_replace("/ ETC.
> always the same, means if you found / you replace it with
say _ and if you found * you
> also replace it with _ or what do you do in the
preg_replace?

No, Stefan, the replacement text depends upon the character
encountered, so no single line is possible.

> Do you use regular expressions in your preg_replace? If
not you can replace it with str_replace.

Yes, we're using regex. For example, if we're testing for
something like "this is a *bolded* word" we would have to
replace the first * with a different tag than the last one,
so the output would be "this is a <b>bolded</b> word".

The only ways I know how to do that are with regular
expressions OR looping through the characters one at a time
taking turns with the *s. The code I've seen do the looping
in PHP is much slower than the code which uses regexs, so
we're going with the latter at this point.

> By the way
> str_replace also enables you to replace different
characters with different replacements
> at once. E.g.:
>
> $text=str_replace(array('/','*','^'),array('replacement
for /','replacement for
> *','replacement for ^'),$text);

Yes, thanks, I've encountered that format. I don't know if
that code is any faster than separate str_replace() calls,
but I can see how it might be. In any event, since we are
using every other '/' and '*' etc. as an opening and closing
tag, I can't see how we could make that work.

I'm not hopeful that there's a better way, but I'm new to
PHP and don't know what I don't know. Thanks for the
feedback!

--

Curtis

Visit We the Thinking
www.wethethinking.com
An online magazine/forum
devoted to philosophical
thinking.

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация