Note that there are some explanatory texts on larger screens.

plurals
  1. POAutomatically convert keywords to links in php
    primarykey
    data
    text
    <p>I am trying to convert specific keywords in text, which are stored in array, to the links.</p> <p>Example text:</p> <pre><code>$text='This text contains many keywords, but also formated &lt;a href="#keywords" title="keywords"&gt;keywords&lt;/a&gt;.' </code></pre> <p>So now I want to convert the word <strong>keywords</strong> to the <code>&lt;a href="#keywords" title="keywords"&gt;#keywords&lt;/a&gt;</code>.</p> <p>I used the very simple preg_replace function</p> <pre><code>preg_replace('/keywords/i',' &lt;a href="#keywords"&gt;keywords&lt;/a&gt; ',$text); </code></pre> <p>but obviously it converts to link also the string <strong>already</strong> formatted as a link, so I get a messy html like:</p> <pre><code>$text='This text contains many &lt;a href="#keywords" title="keywords"&gt;keywords&lt;/a&gt;, but also formated &lt;a href="#&lt;a href="#keywords" title="keywords"&gt;keywords&lt;/a&gt;" title="&lt;a href="#keywords" title="keywords"&gt;keywords&lt;/a&gt;"&gt;&lt;a href="#keywords" title="keywords"&gt;keywords&lt;/a&gt;&lt;/a&gt;.' </code></pre> <p>Expected result:</p> <pre><code>$text='This text contains many &lt;a href="#keywords" title="keywords"&gt;keywords&lt;/a&gt;, but also formated &lt;a href="#keywords" title="keywords"&gt;keywords&lt;/a&gt;.' </code></pre> <p>Any suggestions? THX</p> <p><strong>EDIT</strong></p> <p>We are one step from the perfect function, but still not working well in this case:</p> <pre><code>$text='This text contains many keywords, but also formated &lt;a href="http://www.keywords.com/keywords" title="keywords"&gt;keywords&lt;/a&gt;.' </code></pre> <p>In this case it replaces also the word <code>keywords</code> in the href, so we again get the messy code like</p> <pre><code> &lt;a href="http://www.&lt;a href="http://www.keywords.com/keywords" title="keywords"&gt;keywords&lt;/a&gt;.com/&lt;a href="http://www.keywords.com/keywords" title="keywords"&gt;keywords&lt;/a&gt;" title="keywords"&gt;keywords&lt;/a&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload