Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Advice on ereg_replace() needed

Status
Not open for further replies.

McBugzz

Programmer
Sep 17, 2002
90
JP
I have a sting which looks like ...<a href=&quot;...&quot;>...</a>...<b>...</b>... etc.

What pattern should I use in ereg_replace() function to modify the text which is outside of the tags only?

For example, I have

'This <a href=&quot;...?map=7&quot;>map</a> is here.'

and I want to get

'This <a href=&quot;...?map=7&quot;><b>map</b></a> is here.'
 
Forgive me, I'm not reading into this post as well as you may like....

Do I understand that you want to make your links bold? Couldn't you set up CSS with basic HTML?

<STYLE type=&quot;text/css&quot;>
a { font-weight: bold }
</style>

Plop this in your <head> tags and you could be okay.
 
No! This is ABSOLUTELY NOT WHAT I MEAN!

I'm writing a search engine. It is supposed to find appropriate records in the database. THEN, it's supposed to mark the REQUESTED KEYWORDS in the text, FOR EXAMPLE, with the <b> tag!!! The problem is, if I use simple stuff like str_replace, I can get into a nasty situation when a keyword is found within a TAG (i.e. between < and >), and thus, a TAG get's screwed.

I need to define a pattern for ereg_replace that will NOT affect text between < and >, so that HTML TAGS DON'T GET SCREWED!!!

I hope I have made myself clear now.
 
Could you simply pad the targeted word or phrase with spaces in your expression?

&quot; map &quot; in the sentence &quot;This map is here.&quot; would be affected. But since HTML is immediately next to &quot;map&quot; in the A HREF tag, it would not be affected.

This method would prevent words like &quot;maple&quot; from being highlighted.
 
Dude, what I need is a pattern for ereg_replace function, believe me I have spent a bunch of hours thinking of easier solutions. There are none.
 
Besides, &quot; maps &quot;, &quot; mapping &quot; etc. also should be affected
 
Unless you assign a lot of predefined equivalents or word form variants to 'map' (or whatever your search word is), you are not going to pull off highlighting 'mapping' and not 'maple' - - regardless of whatever reg-ex you create.

'>map<' or ' map' ?
 
Not sure what your whole schema is, so here's a shot in the dark. Assuming your results themselves won't contain HTML, how about running your ereg_replace on the results themselves before injecting it into your final HTML output.

Also, you could loop through the HTML'd string char by char, and turn on a boolean &quot;in_an_html_tag&quot; flag depending on if the current char is a '<' or '>', and drop into your tag-adding function only if the &quot;in_an_html_tag&quot; flag is set to false. Don't know how resource intensive that would get, but it should work.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top