{"id":2445,"date":"2012-07-31T14:44:09","date_gmt":"2012-07-31T20:44:09","guid":{"rendered":"http:\/\/bililite.com\/blog\/?p=2445"},"modified":"2012-07-31T15:00:37","modified_gmt":"2012-07-31T21:00:37","slug":"replace-text-not-in-tags","status":"publish","type":"post","link":"https:\/\/bililite.com\/blog\/2012\/07\/31\/replace-text-not-in-tags\/","title":{"rendered":"Replace Text not in Tags"},"content":{"rendered":"<p>I wanted a way to highlight search terms on the search results page (the way search engines do), by surrounding the text with <code class=\"language-html\">&lt;span class=whatever&gt;&lt;\/span&gt;<\/code>, but simply doing <code class=\"language-php\">preg_replace($re, '&lt;span class=whatever&gt;$0&lt;\/span&gt;', $text)<\/code> replaces things inside any HTML tags as well. There are <a href=\"http:\/\/www.bing.com\/search?setmkt=en-US&q=replace+text+not+in+tags\">some solutions on the web<\/a>, but <a href=\"http:\/\/us.generation-nt.com\/answer\/regex-replace-text-not-inside-html-tag-help-104927732.html\">the simplest one I found<\/a> involves generating a new regular expression. I'd like something I can pass in the regular expression to match and the replacement text, something exactly equivalent to <a href=\"http:\/\/php.net\/preg_replace\">preg_replace<\/a>.<\/p>\r\n<p>What I came up with:<\/p>\r\n<pre><code class=\"language-php\">function preg_replace_text ($pattern, $replacement, $subject){\r\n\t\/\/ only replace text that is not inside tags. Assumes $subject is valid HTML and surrounded by tags\r\n\t\/\/ the (?&lt;=&gt;) is a lookbehind assertion to make sure the match starts with a &gt;\r\n\treturn preg_replace_callback('\/(?&lt;=&gt;)[^&lt;]*\/', function($matches) use ($pattern, $replacement) {\r\n\t\treturn preg_replace($pattern, $replacement, $matches[0]);\r\n\t}, $subject);\r\n}<\/code><\/pre>\r\n<p>Hope this is useful to someone.<\/p>","protected":false},"excerpt":{"rendered":"I wanted a way to highlight search terms on the search results page (the way search engines do), by surrounding the text with &lt;span class=whatever&gt;&lt;\/span&gt;, but simply doing preg_replace($re, '&lt;span class=whatever&gt;$0&lt;\/span&gt;', $text) replaces things inside any HTML tags as well. There are some solutions on the web, but the simplest one I found involves generating [&hellip;]","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"_links":{"self":[{"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/posts\/2445"}],"collection":[{"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/comments?post=2445"}],"version-history":[{"count":10,"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/posts\/2445\/revisions"}],"predecessor-version":[{"id":2455,"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/posts\/2445\/revisions\/2455"}],"wp:attachment":[{"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/media?parent=2445"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/categories?post=2445"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/tags?post=2445"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}