3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php function linkify($text) { $pattern = "/(?:(?:(?<=[^a-z0-9@\xef\xbc\xa0$#\xef\xbc\x83-\xef\xbf\xbf\xe2\x80\xaa-\xe2\x80\xab])|^)(https?:\\/\\/)". "(?:(?:[^ \\f\\r\\t\\n\xb\\0-\x1f\x7f-\xef\xbf\xbf\xe2\x80\xaa-\xe2\x80\xab\x9-\xd\x20\xc2\x85\xc2\xa0". "\xe1\x9a\x80\xe1\xa0\x8e\xe2\x80\x80-\xe2\x80\x8a\xe2\x80\x80-\xe2\x80\x8a\xe2\x80\xa8\xe2\x80\xa9\xe2". "\x80\xaf\xe2\x81\x9f\xe3\x80\x80`!\"#$%&'()*+,.\\/:;<=>?@\\[\\]^_\\\\{|}~-](?:[-]|[^ \\f\\r\\t\\n\xb\\0-". "\x1f\x7f-\xef\xbf\xbf\xe2\x80\xaa-\xe2\x80\xab\x9-\xd\x20\xc2\x85\xc2\xa0\xe1\x9a\x80\xe1\xa0\x8e\xe2\x80". "\x80-\xe2\x80\x8a\xe2\x80\x80-\xe2\x80\x8a\xe2\x80\xa8\xe2\x80\xa9\xe2\x80\xaf\xe2\x81\x9f\xe3\x80\x80`!\"". "#$%&'()*+,.\\/:;<=>?@\\[\\]^_\\\\{|}~-])*)?[^ \\f\\r\\t\\n\xb\\0-\x1f\x7f-\xef\xbf\xbf\xe2\x80\xaa-\xe2". "\x80\xab\x9-\xd\x20\xc2\x85\xc2\xa0\xe1\x9a\x80\xe1\xa0\x8e\xe2\x80\x80-\xe2\x80\x8a\xe2\x80\x80-\xe2\x80". "\x8a\xe2\x80\xa8\xe2\x80\xa9\xe2\x80\xaf\xe2\x81\x9f\xe3\x80\x80`!\"#$%&'()*+,.\\/:;<=>?@\\[\\]^_\\\\{|". "}~-]\\.)+(?:(?:(?:aero|asia|biz|cat|com|coop|edu|gov|info|int|jobs|mil|mobi|museum|name|net|org|pro|tel|". "travel|xxx)(?=[^a-z0-9]|$))|(?:(?:ac|ad|ae|af|ag|ai|al|am|an|ao|aq|ar|as|at|au|aw|ax|az|ba|bb|bd|be|bf|bg". "|bh|bi|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|cr|cs|cu|cv|cx|cy|cz|dd|de|dj". "|dk|dm|do|dz|ec|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gg|gh|gi|gl|gm|gn|gp|gq|gr|gs|gt|gu". "|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|in|io|iq|ir|is|it|je|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb". "|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|mv|mw|mx|my|mz|na|nc|ne|nf". "|ng|ni|nl|no|np|nr|nu|nz|om|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|ps|pt|pw|py|qa|re|ro|rs|ru|rw|sa|sb|sc|sd|se|sg". "|sh|si|sj|sk|sl|sm|sn|so|sr|ss|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tl|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk". "|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|za|zm|zw)(?=[^a-z0-9]|$)))|(?:(?<![-_.\\/])|^)(?:(?:[a-z0-9](?:". "[-]|[a-z0-9])*)?[a-z0-9]\\.)+(?:(?:aero|asia|biz|cat|com|coop|edu|gov|info|int|jobs|mil|mobi|museum|name|". "net|org|pro|tel|travel|xxx)(?=[^a-z0-9]|$))|(?:(?<![-_.\\/])|^)(?:(?:[a-z0-9](?:[-]|[a-z0-9])*)?[a-z0-9]\\". ".){2,}(?:(?:ac|ad|ae|af|ag|ai|al|am|an|ao|aq|ar|as|at|au|aw|ax|az|ba|bb|bd|be|bf|bg|bh|b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9". "\x8f\xc9\x93-\xc9\x94\xc9\x96-\xc9\x97\xc9\x99\xc9\x9b\xc9\xa3\xc9\xa8\xc9\xaf\xc9\xb2\xca\x89\xca\x8b\xca". "\xbb\xcc\x80-\xcd\xaf\xe1\xb8\x80-\xe1\xbb\xbf]+\\/))*)?(?:\\?[-a-z0-9!?\\*'\\(\\);:&=\\+\\$\\/%#\\[\\]_\\". ".,~|]*[a-z0-9_&=#\\/])?/i" ; return preg_replace_callback($pattern, function ($matches) { if (empty($matches[1])) { $matches[0] = 'http://'.$matches[0]; } if (preg_match('/(?:\\.jpe?g|png|gif)\\Z/i', $matches[0])) { $format = '<a href="%s">%s</a>'; } else { $format = '<img src="%s" />'; } return sprintf($format, $matches[0], $matches[0]); }, $text); } $test = <<< 'EOD' hoge.com hoge.jp hoge.co.jp hoge.com/index.html hoge.jp/index.html hoge.co.jp/index.html http://hoge.com/index.html http://hoge.jp/index.html http://hoge.co.jp/index.html https://hoge.com/index.html https://hoge.jp/index.html https://hoge.co.jp/index.html http://hoge.com/test.jpg http://hoge.com/test.JpEg http://hoge.com/test.pnG EOD; echo linkify($test);
Output for 5.3.0 - 5.6.28, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
<img src="http://hoge.com" /> hoge.jp <img src="http://hoge.co.jp" /> <img src="http://hoge.com/index.html" /> hoge.jp/index.html <img src="http://hoge.co.jp/index.html" /> <img src="http://hoge.com/index.html" /> <img src="http://hoge.jp/index.html" /> <img src="http://hoge.co.jp/index.html" /> <img src="https://hoge.com/index.html" /> <img src="https://hoge.jp/index.html" /> <img src="https://hoge.co.jp/index.html" /> <a href="http://hoge.com/test.jpg">http://hoge.com/test.jpg</a> <a href="http://hoge.com/test.JpEg">http://hoge.com/test.JpEg</a> <a href="http://hoge.com/test.pnG">http://hoge.com/test.pnG</a>
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.2.17
Parse error: syntax error, unexpected T_FUNCTION in /in/3aXc3 on line 54
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1, 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_FUNCTION in /in/3aXc3 on line 54
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/3aXc3 on line 54
Process exited with code 255.