How to replace multiple values in php

This should work for you: <?php $string = “test1 test1 test2 test2 test2 test1 test1 test2”; echo $string . “<br />”; echo $string = strtr($string, array(“test1” => “test2”, “test2” => “test1”)); ?> Output: test1 test1 test2 test2 test2 test1 test1 test2 test2 test2 test1 test1 test1 test2 test2 test1 Checkout this DEMO: http://codepad.org/b0dB95X5

Regex ignore URL already in HTML tags

Try this (?<!href=”)(\b[\w]+:\/\/[\w-?&;#~=\.\/\@]+[\w\/]) See it here on Regexr To make it more general you can simplify your lookbehind to check only for “=”” (?<!=”)(\b[\w]+:\/\/[\w-?&;#~=\.\/\@]+[\w\/]) See it on Regexr (?<!href=”https://stackoverflow.com/questions/9567836/) is a negative lookbehind assertion, it ensures that there is no”href=”” before your pattern. \b is a word boundary that anchors the start of your link … Read more

PHP Preg-Replace more than one underscore

The + operator (quantifier) matches multiple instances of the last character (, character class or capture group or back-reference). $string = preg_replace(‘/_+/’, ‘_’, $string); This would replace one or more underscores with a single underscore. Technically more correct to the title of the question then is to only replace two or more: $string = preg_replace(‘/__+/’, … Read more

Extract first integer in a string with PHP

A convenient (although not record-breaking in performance) solution using regular expressions would be: $string = “3rd time’s a charm”; $filteredNumbers = array_filter(preg_split(“/\D+/”, $string)); $firstOccurence = reset($filteredNumbers); echo $firstOccurence; // 3 Assuming that there is at least one number in the input, this is going to print the first one. Non-digit characters will be completely ignored … Read more

Why preg_replace throws me a “Unknown modifier” error? [duplicate]

You are using forward-slashes as your regex pattern delimeter, so /<title>(.*)</title>/Ui’ will not work (</title> has a forward slash). You should be able to escape the forward slash or use a different delimiter that is not contained within the pattern, for example ‘/<title>(.*)<\/title>/Ui’ //(esacaping) or ‘~<title>(.*)</title>~Ui’ //different delimiter

regular expression and forward slash

Try to begin and end your regular expression with different sign than / I personally use ` I’ve seen people using # I think most chars are good. You can read more about it here: http://pl.php.net/manual/en/regexp.reference.delimiters.php Like this: preg_match(‘#time/emit#’, $subject); // instead of /time/emit/ To put it another way: Your $find variable should contain rather … Read more

How to properly escape a backslash to match a literal backslash in single-quoted and double-quoted PHP regex patterns

A backslash character (\) is considered to be an escape character by both PHP’s parser and the regular expression engine (PCRE). If you write a single backslash character, it will be considered as an escape character by PHP parser. If you write two backslashes, it will be interpreted as a literal backslash by PHP’s parser. … Read more

Replace deprecated preg_replace /e with preg_replace_callback [duplicate]

You can use an anonymous function to pass the matches to your function: $result = preg_replace_callback( “/\{([<>])([a-zA-Z0-9_]*)(\?{0,1})([a-zA-Z0-9_]*)\}(.*)\{\\1\/\\2\}/isU”, function($m) { return CallFunction($m[1], $m[2], $m[3], $m[4], $m[5]); }, $result ); Apart from being faster, this will also properly handle double quotes in your string. Your current code using /e would convert a double quote ” into \”.

Replace tabs and spaces with a single space as well as carriage returns and newlines with a single newline

First, I’d like to point out that new lines can be either \r, \n, or \r\n depending on the operating system. My solution: echo preg_replace(‘/[ \t]+/’, ‘ ‘, preg_replace(‘/[\r\n]+/’, “\n”, $string)); Which could be separated into 2 lines if necessary: $string = preg_replace(‘/[\r\n]+/’, “\n”, $string); echo preg_replace(‘/[ \t]+/’, ‘ ‘, $string); Update: An even better … Read more