views:

118

answers:

3

I am using PHP. I have some articles stored in MySQL and they all have some style (like with some css or tags). Now I have to add some links to some text randomly and automatically. This means I have to add links to the text and make sure the links do not break the style.

I think the most difficult part is how to add links but not to break the style. Are there any rules or example I can follow to make this in PHP?

Any help and advices is great appreciated~

Updated:

Maybe this question is how to choose the "right text" to add links. If the text is already in a link, it is not suitable. So what should I do about this?

For example:

<div>This is an article with some <a href="http://originallinks.com"&gt;original links</a></div>

I need to choose some text to add links. The text is chosen randomly by program. If the text is "article":

<div>This is an <a href="http://anotherlink.com"&gt;article&lt;/a&gt; with some <a htef="originallinks.com">original links</a></div>

This will be OK, but if the text is "original":

<div>This is an article with some <a htef="originallinks.com"><a href="http://anotherlink.com"&gt;original&lt;/a&gt; links</a></div>

This would be bad cause the link was added into another link.

So my problem is how to choose the right text to add(this process is randomly and automatically). What should I take into consideration to make this?

+1  A: 

If you create a css style for the links, where you specify all the properties (color, size...) I don't see how it could break your style, as you just tag the text with the correct markup.

Let's say you have :

<div>Some text with style and a link and some more text</div>

It will become

<div>Some text with style and <a style="link-style">a link</a> and some more text</div>

I don't see where it can break your style...

Blackethylene
as long, as you take care and close all your tags again...
Boldewyn
I mean the tags. I insert "<a...>...</a>" tags to the text randomly so I have to make sure the "<a...>...</a>" tags won't be inserted into a "<a...>...</a>"again.That's just one example and I don't know what else tags should I care and what technique should I choose to do this.(Regular expression or DOM?)
Mickey Shine
Have you ever noticed these highlighted terms, when you come from a google result to some page? They do it in JS/DOM. I once made the attempt to make something like this in PHP, but got into the very same problems like you, eg. entering tags in attributes and the such. I'd recommend adding the links via JS, if that is an option for you, but you haven't specified that in your question.
Boldewyn
@Boldewyn, if JS/DOM is feasible (but I never tried that), it would be a feasible way in PHP too. Doesn't PHP also have DOM?
Mickey Shine
A: 

I understood that you want your links to be undistinguished from other test in the same place. In that case, you need to explicitly specify a style for links that's similar to your text style.

Example:

div.mycontent {
    color: #333333;
    font-weight: bold;
}
div.mycontent a {          // add this for links
    color: #333333;
    font-weight: bold;
    text-decoration: none;
}
Makram Saleh
Thank you. But I am concerning the tags, I mean the link I added will not break the tags. Do you have any suggestions?
Mickey Shine
A: 

Are you inserting the link tags using php? Could be solved by having multiple passes, after you go through adding links to "article", then use explode() on both the start and end tags to separate out the parts that are already links, and exclude those from having tags added to them.

Might be a better way to do this using regular expressions, using explode you might get a headache trying to keep track of which parts of the array are the links. Also, you'd have to deal with recombining the thing afterwards and inserting the tags back.

Karl