You are here: Re: Complex replace in php 4 « PHP Programming Language « IT news, forums, messages
Re: Complex replace in php 4

Posted by Jon Slaughter on 05/01/07 00:35

"Fabri" <farsi.i.cazzi.propri@mai.eh> wrote in message
news:46366a73$0$4788$4fafbaef@reader4.news.tin.it...
>I searched and tried to develop (with no luck) a function to do the
>following:
>
>
> I have a string that may be:
>
> "Le'ts go to <a href="my.htm">my car</a>. Tomorrow I'll have to buy a new
> car. My new car is <em>red</em>! Please don't think to be in Nascar!!"
>
>
> What I have to do is replace occurences of "car" with <a
> href="/...">car</a> BUT in these cases:
>
> - if there is already a wrapped link
> - if car is part of another word
>
>
> Also, I'm using php4 so I can't use str_ireplace for case insensitive
> replace.
>
> Can you help me?
>

you want a basic recursive parser but its probably overly complicated for
what you need.

Its better if you can add some structural information to the tag that will
be ignored by html. This will help you in more efficiently searching for
car.

You could do something like

> "Le'ts go to <a href="my.htm">my car</a>. Tomorrow I'll have to buy a new
> car. My new <car>car</car> is <em>red</em>! Please don't think to be in
> Nascar!!"

and then just search for <car>car</car> and replace it with the link.

You could also do something like

<span class="MyNewCar">car</span>

and essentially do the same with the added bonus that you can modify the
style using css.

i.e., search for <span class="MyNewCar"> and replace it with

<a href="..."><span class="MyNewCar">car</span></a>


Now if you can do the processing off line you want to write a simple
recusive parser. What you do here is search for all instances of cars and
then search backwards to make sure they are not contained in any <a href>
tags. The issue here is that theoretically it could take very long to do
this.

Since you are making car something special I would imagine you could just
add some structural information to it to make it special. If you are worried
about apply the same thing twice so you get something like

<a href="..."><a href="..."><span class="MyNewCar">car</span></a></a>

then its pretty easy to check to prevent that.

I would suggest you play around with it using simple examples and see what
you come up with. Its essentially just searching and I don't think you'll
need more than that. (and I doubt you'll need regular expressions)

Jon

 

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

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