I can easily insert the span commands but this inserts them everywhere (including links).
A result may look like:
---
Widget (Product Code)
This widget is one that you can buy.
You may be interested in <a href='cgi-bin/showproducts.pl?bluewidget,redwidget,greenwidget' target='content'>a bluewidget, a redwidget or even a greenwidget</a>
---
Each result (i.e. all of the above) is stored in a variable $pdesc.
When I swap the word 'widget' (if that is what the user searched for, it corrupts the link as showproducts.pl?bluewidget becomes showproducts.pl?blue<span class=...>widget</span>
So what I want is to replace 'widget' globally within the string, but not if it is between the < and > symbols. Is it possible to do this?
I couldn't figure it out:
$pdesc =~ s/($search)/<span class=...>$1<\/span>/gi;
is what I have used to replace all occurences. What else do I need to add to do what I want. Bearing in mind that it may contain many < and >'s for various links and formatting.
Widget (Product Code)<br>This widget is one that you can buy.<br>You may be interested in <a href='cgi-bin/showproducts.pl?bluewidget,redwidget,greenwidget' target='content'>a bluewidget, a redwidget or even a greenwidget</a>
Another variable is what the user searches on ($search) and may contain:
widget
I want to adjust $pdesc to become:
<span class="...">Widget</span> (Product Code)<br>This <span class="...">widget</span> is one that you can buy.<br>You may be interested in <a href='cgi-bin/showproducts.pl?bluewidget,redwidget,greenwidget' target='content'>a blue<span class="...">widget</span>, a red<span class="...">widget</span> or even a green<span class="...">widget</span></a>
Changes in bold. Performing the above would be simple, however notice href (in italics) which contains widget several times, but I obviously do not want to surround that with the <span...> commands. I want to replace all occurences of widget except those between any '<' and '>'.
If your $pdesc content is being put together inside a function (it looks like it's running through a data set and building a set of links, so I'm assuming it's returned by a function or could be), could you do the substitution inside the function by passing the search term to it. At that point I'm assuming that the product descriptions and the links to them are separated so there's no problem is there?
Otherwise, since I stink at regex, I would probably end up writing a function that parsed the text and then returned it marked up as needed, but I'm sure there's a better solution.
Tom
$pdesc = "Widget (Product Code)<br>This widget is one that you can buy.<br>You may be interested in <a href='cgi-bin/ showproducts.pl?bluewidget,redwidget,greenwidget' target='content'>a bluewidget, a redwidget or even a greenwidget</a>";$pdesc =~ s/widget/<span class=\"\.\.\.\">widget<\/span>/ig;
&fix;
sub fix
{$pdesc =~ s/<a href(.*)<span class=\"\.\.\.\">widget<\/span>(.*)target='content'>/<a href$1widget$2target='content'>/ig;
if ($pdesc =~ /<a href(.*)<span class(.*)<\/span>(.*)target='content'>/i)
{&fix;}}
It does work, but I'm sure someone can come up with a simpiler solution.
[webmasterworld.com...]