Replace preg_replace() e modifier with preg_replace_callback

In a regular expression, you can “capture” parts of the matched string with (brackets); in this case, you are capturing the (^|_) and ([a-z]) parts of the match. These are numbered starting at 1, so you have back-references 1 and 2. Match 0 is the whole matched string. The /e modifier takes a replacement string, … Read more

Remove a part of a string, but only when it is at the end of the string

You’ll note the use of the $ character, which denotes the end of a string: $new_str = preg_replace(‘/string$/’, ”, $str); If the string is a user supplied variable, it is a good idea to run it through preg_quote first: $remove = $_GET[‘remove’]; // or whatever the case may be $new_str = preg_replace(“https://stackoverflow.com/”. preg_quote($remove, “https://stackoverflow.com/”) . … Read more

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

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