Asked  7 Months ago    Answers:  5   Viewed   31 times

I need to detect whether a string contains HTML tags.

if(!preg_match('(?<=<)w+(?=[^<]*?>)', $string)){ 
    return $string;

The above regex gives me an error:

preg_match() [function.preg-match]: Unknown modifier ''

I'm not well up on regex so not sure what the problem was. I tried escaping the and it didn't do anything.

Is there a better solution than regex? If not, what would be the correct regex to work with the preg_match?



A simple solution is:

if($string != strip_tags($string)) {
    // contains HTML

The benefit of this over a regex is it's easier to understand, however I could not comment on the speed of execution of either solution.

Wednesday, March 31, 2021
answered 7 Months ago

Here is a function i've used before, which works pretty well:

function closetags($html) {
    preg_match_all('#<(?!meta|img|br|hr|inputb)b([a-z]+)(?: .*)?(?<![/|/ ])>#iU', $html, $result);
    $openedtags = $result[1];
    preg_match_all('#</([a-z]+)>#iU', $html, $result);
    $closedtags = $result[1];
    $len_opened = count($openedtags);
    if (count($closedtags) == $len_opened) {
        return $html;
    $openedtags = array_reverse($openedtags);
    for ($i=0; $i < $len_opened; $i++) {
        if (!in_array($openedtags[$i], $closedtags)) {
            $html .= '</'.$openedtags[$i].'>';
        } else {
            unset($closedtags[array_search($openedtags[$i], $closedtags)]);
    return $html;

Personally though, I would not do it using regexp but a library such as Tidy. This would be something like the following:

$str = '<p>This is some text and here is a <strong>bold text then the post stop here....</p>';
$tidy = new Tidy();
$clean = $tidy->repairString($str, array(
    'output-xml' => true,
    'input-xml' => true
echo $clean;
Wednesday, March 31, 2021
answered 7 Months ago

You should point to your vendor/autoload.php at Settings | PHP | PHPUnit when using PHPUnit via Composer.

This blog post has all the details (with pictures) to successfully configure IDE for such scenario:

Related usability ticket:

P.S. The WI-18388 ticket is already fixed in v8.0

Wednesday, March 31, 2021
answered 7 Months ago

On Mac OS X environment variables available in Terminal and for the normal applications can be different, check the related question for the solution how to make them similar.

Note that this solution will not work on Mountain Lion (10.8).

Saturday, May 29, 2021
answered 5 Months ago


A ready-to-use element for Dart Polymer 1.0 is bwu-bind-html


Polymer now provides support for this out of the box

 this.injectBoundHTML('<div>your HTML goes here ${someBoundFieldValue}</div>);


This is the code of the <safe-html> tag I'm using.

library safe_html;

import 'dart:async';
import "dart:html";

import "package:polymer/polymer.dart";

class SafeHtml extends PolymerElement  {

  @published String model;

  NodeValidator nodeValidator;
  bool get applyAuthorStyles => true;
  bool isInitialized = false;

  SafeHtml.created() : super.created() {
    nodeValidator = new NodeValidatorBuilder()

  void modelChanded(old) {
    if(isInitialized) {

  void _addFragment() {
    var fragment = new DocumentFragment.html(model, validator: nodeValidator);


  void attached() {
    super.attached(); {
      isInitialized = true;
<!DOCTYPE html>

<polymer-element name="safe-html"> 
    <div id="container"></div>

  <script type="application/dart" src='safe_html.dart'></script>



<safe-html model="{{someField}}></safe-html>
Wednesday, June 9, 2021
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :