Note that there are some explanatory texts on larger screens.

plurals
  1. POMaking a regex with a lookbehind
    primarykey
    data
    text
    <p>I have a regex that replaces the usual hyphen with the non-break hyphen, if it’s found inside of a <em>word</em> </p> <pre><code>preg_replace('@(?&lt;= \w)-(?= \w)@xu','‑',$string); </code></pre> <p>But there are strings like <code>"this is a link":http://example.com/funny-stuff-is-funny</code>, which are get parsed too. The problem is further parsing such string breaks because of the non-break hyphen inside of the link. So I need or to modify the original regex to not match links, or to cover the whole function in another where I will fix that behavior. The first way I tried to go ended at this</p> <pre><code>'@((?&lt;! http)[/\pL\pP]+\w)-(?= \w)@xu' </code></pre> <p>It matches <code>link":http://example.com/funny-</code> and still replaces the hyphen. So I went the second way and get the following thing:</p> <pre><code>preg_replace( /* Fixing broken links with NON-BREAKING HYPHEN */ '@(https?\://[^\s]+)‑@', '$1===-===', /* This is to make a better view */ preg_replace( /* NON-BREAKING HYPHEN inside of a word */ '@(?&lt;= \w)-(?= \w)@xu', '‑', $string)))); </code></pre> <p>And with this we can have replaced hyphen in links. A single one hyphen:</p> <pre><code>"this is a link":http://example.com/funny-things-are===-===funny </code></pre> <p>I’ve tried to change ‘greediness’ of the covering expression with <code>‘U’</code> but it doesn’t seem to fix it, so I’m asking for help.</p> <p><strong>===upd===</strong></p> <p>I accepted solution with <code>preg_replace_callback()</code> as a general idea, but I had to use <code>preg_replace()</code> inside of it because of multibyte encoding.</p>
    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.
    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