Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I managed to do what I wanted (<strong>without using Regex</strong>) by: </p> <ul> <li>parsing each character of my string</li> <li>removing all <code>&lt;a&gt;</code> tags (copying them to a temporary array and keeping a placeholder on the string)</li> <li><code>str_replace</code> the new string in order to replace all the keywords</li> <li>repopulating the placeholders by it's original <code>&lt;a&gt;</code> tags</li> </ul> <p>Here's the code I used, in case someone else needs it:</p> <pre><code>$str = &lt;&lt;&lt;STRA Moses supposes his toeses are roses, but &lt;a href="original-moses1.html"&gt;Moses&lt;/a&gt; supposes erroneously; for nobody's toeses are posies of roses, as Moses supposes his toeses to be. Ganda &lt;span class="cenas"&gt;&lt;a href="original-moses2.html" target="_blank"&gt;Moses&lt;/a&gt;&lt;/span&gt;! STRA; $arr1 = str_split($str); $arr_links = array(); $phrase_holder = ''; $current_a = 0; $goto_arr_links = false; $close_a = false; foreach($arr1 as $k =&gt; $v) { if ($close_a == true) { if ($v == '&gt;') { $close_a = false; } continue; } if ($goto_arr_links == true) { $arr_links[$current_a] .= $v; } if ($v == '&lt;' &amp;&amp; $arr1[$k+1] == 'a') { /* &lt;a */ // keep collecting every char until &lt;/a&gt; $arr_links[$current_a] .= $v; $goto_arr_links = true; } elseif ($v == '&lt;' &amp;&amp; $arr1[$k+1] == '/' &amp;&amp; $arr1[$k+2] == 'a' &amp;&amp; $arr1[$k+3] == '&gt;' ) { /* &lt;/a&gt; */ $arr_links[$current_a] .= "/a&gt;"; $goto_arr_links = false; $close_a = true; $phrase_holder .= "{%$current_a%}"; /* put a parameter holder on the phrase */ $current_a++; } elseif ($goto_arr_links == false) { $phrase_holder .= $v; } } echo "Links Array:\n"; print_r($arr_links); echo "\n\n\nPhrase Holder:\n"; echo $phrase_holder; echo "\n\n\n(pre) Final Phrase (with my keyword replaced):\n"; $final_phrase = str_replace("Moses", "&lt;a href=\"novo-mega-link.php\"&gt;Moses&lt;/a&gt;", $phrase_holder); echo $final_phrase; echo "\n\n\nFinal Phrase:\n"; foreach($arr_links as $k =&gt; $v) { $final_phrase = str_replace("{%$k%}", $v, $final_phrase); } echo $final_phrase; </code></pre> <p>The output:</p> <p>Links Array:</p> <pre><code>Array ( [0] =&gt; &lt;a href="original-moses1.html"&gt;Moses&lt;/a&gt; [1] =&gt; &lt;a href="original-moses2.html" target="_blank"&gt;Moses&lt;/a&gt; ) </code></pre> <p>Phrase Holder:</p> <pre><code>Moses supposes his toeses are roses, but {%0%} supposes erroneously; for nobody's toeses are posies of roses, as Moses supposes his toeses to be. Ganda &lt;span class="cenas"&gt;{%1%}&lt;/span&gt;! </code></pre> <p>(pre) Final Phrase (with my keyword replaced):</p> <pre><code>&lt;a href="novo-mega-link.php"&gt;Moses&lt;/a&gt; supposes his toeses are roses, but {%0%} supposes erroneously; for nobody's toeses are posies of roses, as &lt;a href="novo-mega-link.php"&gt;Moses&lt;/a&gt; supposes his toeses to be. Ganda &lt;span class="cenas"&gt;{%1%}&lt;/span&gt;! </code></pre> <p><strong>Final Phrase:</strong></p> <pre><code>&lt;a href="novo-mega-link.php"&gt;Moses&lt;/a&gt; supposes his toeses are roses, but &lt;a href="original-moses1.html"&gt;Moses&lt;/a&gt; supposes erroneously; for nobody's toeses are posies of roses, as &lt;a href="novo-mega-link.php"&gt;Moses&lt;/a&gt; supposes his toeses to be. Ganda &lt;span class="cenas"&gt;&lt;a href="original-moses2.html" target="_blank"&gt;Moses&lt;/a&gt;&lt;/span&gt;! </code></pre>
 

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