PHP replacing special characters like à->a, è->e

There’s a much easier way to do this, using iconv – from the user notes, this seems to be what you want to do: characters transliteration // PHP.net User notes <?php $string = “ʿABBĀSĀBĀD”; echo iconv(‘UTF-8’, ‘ISO-8859-1//TRANSLIT’, $string); // output: [nothing, and you get a notice] echo iconv(‘UTF-8’, ‘ISO-8859-1//IGNORE’, $string); // output: ABBSBD echo iconv(‘UTF-8’, … Read more

What Delimiter to use for preg_replace in PHP (replace working outside of PHP but not inside)

preg_replace() requires a delimiter character: preg_replace(“/$pat/” … Traditionally it’s the forward slash, but it can be any character – especially when you need the forward slash in the regex itself you can resort to another character. This flexibility allows you to express “/http:\/\/foo\/bar\//” (“leaning toothpick syndrome”) as “!http://foo/bar/!”. The delimiter character is necessary to separate … Read more

Extract URL from string

John Gruber has spent a fair amount of time perfecting the “one regex to rule them all” for link detection. Using preg_replace() as mentioned in the other answers, using the following regex should be one of the most accurate, if not the most accurate, method for detecting a link: (?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'”.,<>?«»“”‘’])) If you only wanted to … Read more

Replace multiple newlines, tabs, and spaces [duplicate]

In theory, you regular expression does work, but the problem is that not all operating system and browsers send only \n at the end of string. Many will also send a \r. Try: I’ve simplified this one: preg_replace(“/(\r?\n){2,}/”, “\n\n”, $text); And to address the problem of some sending \r only: preg_replace(“/[\r\n]{2,}/”, “\n\n”, $text); Based on … Read more

Java equivalent to PHP’s preg_replace_callback

Trying to emulate PHP’s callback feature seems an awful lot of work when you could just use appendReplacement() and appendTail() in a loop: StringBuffer resultString = new StringBuffer(); Pattern regex = Pattern.compile(“regex”); Matcher regexMatcher = regex.matcher(subjectString); while (regexMatcher.find()) { // You can vary the replacement text for each match on-the-fly regexMatcher.appendReplacement(resultString, “replacement”); } regexMatcher.appendTail(resultString);

Convert plain text URLs into HTML hyperlinks in PHP

Here is another solution, This will catch all http/https/www and convert to clickable links. $url=”~(?:(https?)://([^\s<]+)|(www\.[^\s<]+?\.[^\s<]+))(?<![\.,:])~i”; $string = preg_replace($url, ‘<a href=”$0″ target=”_blank” title=”$0″>$0</a>’, $string); echo $string; Alternatively for just catching http/https then use the code below. $url=”/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/”; $string= preg_replace($url, ‘<a href=”$0″ target=”_blank” title=”$0″>$0</a>’, $string); echo $string; EDIT: The script below will catch all URL types and … Read more

Ignore html tags in preg_replace

I assume you should make your function based on DOMDocument and DOMXPath rather than using regular expressions. Even those are quite powerful, you run into problems like the one you describe which are not (always) easily and robust to solve with regular expressions. The general saying is: Don’t parse HTML with regular expressions. It’s a … Read more