Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><em>I'll let others go for regex solutions. I'll propose something that hopefully more readable.</em></p> <p>The following code uses my <a href="https://github.com/theraot/paladio/blob/master/paladio/parser.lib.php" rel="nofollow">Parser class from Paladio</a> (it's under CC-BY 3.0), it works on UTF-8. Just download the <a href="https://raw.github.com/theraot/paladio/master/paladio/parser.lib.php" rel="nofollow">raw version of the library</a>, it is made to have no dependencies.</p> <p>The cde is explained in the comments:</p> <pre><code>&lt;?php $string = "Hello how are ##you ? I want ##join you."; // now I want replace all ##.... words with &lt;a&gt;##.....&lt;/a&gt; //Create a parser object for the string $parser = new Parser($string); //Create a variable to hold the result $result = ''; //While we haven't reached the end of the string while($parser-&gt;CanConsume()) { //Take all the text until the next '##' or the end of the string // and add to the result the string taken $result .= $parser-&gt;ConsumeUntil('##'); //If we take a '##' if ($parser-&gt;Consume('##')) { //Take the text until the next whitespace or new line $tag = $parser-&gt;ConsumeUntil(array(' ', "\t", "\r", "\n")); //Add the new converted text to the result $result .= '&lt;a&gt;###'.$tag.'&lt;/a&gt;'; } } // example Hello how are &lt;a&gt;##you&lt;/a&gt; ? I want &lt;a&gt;##join&lt;/a&gt;you. echo $result; ?&gt; </code></pre> <hr> <p>Based on the comments, this is a modified version that will allow to detect words marked with any of the given strings (<code>'##'</code> and <code>'**'</code> in the example):</p> <pre><code>function autolink($ptext, $detc) { // declared whitespace for readability and performance $whitespace = array(' ', "\t", "\r", "\n"); $parser = new Parser($ptext); $result = ''; while($parser-&gt;CanConsume()) { $result .= $parser-&gt;ConsumeUntil($detc); if ($parser-&gt;Consume($detc)) { $newtag = $parser-&gt;ConsumeUntil($whitespace); $result .= '&lt;a href='.$newtag.'&gt;'.$newtag.'&lt;/a&gt;'; } } return $result; } </code></pre> <p>Example usage:</p> <pre><code>echo autolink("Hello how are ##you ? I want **join you.", array('##', '**')); </code></pre> <p>Outputs:</p> <pre><code>Hello how are &lt;a href=you&gt;you&lt;/a&gt; ? I want &lt;a href=join&gt;join&lt;/a&gt; you. </code></pre> <p><em>Tested on my local server</em>.</p> <hr> <p><em>Notes</em>:</p> <p>1) The instruction <code>$parser-&gt;Consume($detc)</code> will return the found string, so you can use it to branch, example:</p> <pre><code>$input = $parser-&gt;Consume(array('a', 'b')); if ($input === 'a') { // do something } else if ($input === 'b') { // do something else } else /*if ($input === null)*/ { // fallback case } </code></pre> <p>2) The supported things to <code>Consume</code> are:</p> <ol> <li>Given strings.</li> <li>Arrays of strings. </li> <li>Numbers (amount of characters).</li> <li>Null (will consume a single character).</li> </ol> <p>3) <code>Parser</code> uses <code>mb_*</code> functions for some of the operations^, and expects <code>UTF-8</code>. If you experience problems with encoding you want to call <code>mb_internal_encoding('UTF-8');</code> before using <code>Parser</code> and convert your string to UTF-8 (I recommend <a href="http://www.php.net/manual/en/function.iconv.php" rel="nofollow"><code>iconv</code></a> for this operation). ^: Some other parts are optimized using byte per byte operations.</p>
 

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