Remove multiple whitespaces

You need: $ro = preg_replace(‘/\s+/’, ‘ ‘, $row[‘message’]); You are using \s\s+ which means whitespace(space, tab or newline) followed by one or more whitespace. Which effectively means replace two or more whitespace with a single space. What you want is replace one or more whitespace with single whitespace, so you can use the pattern \s\s* … Read more

php regex to match outside of html tags

You can use an assertion for that, as you just have to ensure that the searched words occur somewhen after an >, or before any <. The latter test is easier to accomplish as lookahead assertions can be variable length: /(asf|foo|barr)(?=[^>]*(<|$))/ See also http://www.regular-expressions.info/lookaround.html for a nice explanation of that assertion syntax.

Replacing accented characters php

I have tried all sorts based on the variations listed in the answers, but the following worked: $unwanted_array = array( ‘Š’=>’S’, ‘š’=>’s’, ‘Ž’=>’Z’, ‘ž’=>’z’, ‘À’=>’A’, ‘Á’=>’A’, ‘Â’=>’A’, ‘Ã’=>’A’, ‘Ä’=>’A’, ‘Å’=>’A’, ‘Æ’=>’A’, ‘Ç’=>’C’, ‘È’=>’E’, ‘É’=>’E’, ‘Ê’=>’E’, ‘Ë’=>’E’, ‘Ì’=>’I’, ‘Í’=>’I’, ‘Î’=>’I’, ‘Ï’=>’I’, ‘Ñ’=>’N’, ‘Ò’=>’O’, ‘Ó’=>’O’, ‘Ô’=>’O’, ‘Õ’=>’O’, ‘Ö’=>’O’, ‘Ø’=>’O’, ‘Ù’=>’U’, ‘Ú’=>’U’, ‘Û’=>’U’, ‘Ü’=>’U’, ‘Ý’=>’Y’, ‘Þ’=>’B’, ‘ß’=>’Ss’, ‘à’=>’a’, … Read more

How can I convert ereg expressions to preg in PHP?

The biggest change in the syntax is the addition of delimiters. ereg(‘^hello’, $str); preg_match(‘/^hello/’, $str); Delimiters can be pretty much anything that is not alpha-numeric, a backslash or a whitespace character. The most used are generally ~, / and #. You can also use matching brackets: preg_match(‘[^hello]’, $str); preg_match(‘(^hello)’, $str); preg_match(‘{^hello}’, $str); // etc If … Read more

Replace URLs in text with HTML links

Let’s look at the requirements. You have some user-supplied plain text, which you want to display with hyperlinked URLs. The “http://” protocol prefix should be optional. Both domains and IP addresses should be accepted. Any valid top-level domain should be accepted, e.g. .aero and .xn--jxalpdlp. Port numbers should be allowed. URLs must be allowed in … Read more