preg-replace

How to do replacement only on unquoted parts of a string?

大兔子大兔子 提交于 2020-01-05 04:30:11
问题 How would I best achieve the following: I would like to find and replace values in a string in PHP unless they are in single or double quotes. EG. $string = 'The quoted words I would like to replace unless they are "part of a quoted string" '; $terms = array( 'quoted' => 'replaced' ); $find = array_keys($terms); $replace = array_values($terms); $content = str_replace($find, $replace, $string); echo $string; echo 'd string should return: 'The replaced words I would like to replace unless they

using preg_replace to replace a <br />

烂漫一生 提交于 2020-01-05 03:57:09
问题 I've read a few articles on preg_replace and still don't understand what all the weird ]{!('/)[ characters mean. Basically, I want to find the first instance of a break <br /> , and replace it with a </strong><br /> Code I have: preg_replace('<br />', '</strong><br />', nl2br($row['n_message']), 1) but I know I'm missing something in how I declare the strings <br /> and </strong> . Any help? Thanks. 回答1: Regular Expressions The only thing you are missing is a delimiter in your regexp pattern.

how to implement template with conditions with preg_replace

≯℡__Kan透↙ 提交于 2020-01-04 14:00:55
问题 I'm trying to implement an admin interface where manager could create advanced rules of site meta tags formation. I have a function which takes a template and replaces placeholders in it with values from $registry and applies modifiers if needed. $registy = array( 'profession_name' => 'actor', 'total_found' => 100, ); $result = preg_replace_callback('/\{([^{}:]+)(:[^{}:]+)?\}/', function($matches) use ($registry) { $result = $registry[$matches[1]] ?: $matches[0]; if (in_array($matches[2],

Avoiding SQL Injection in SQLite3

☆樱花仙子☆ 提交于 2020-01-04 06:04:17
问题 I'm trying to figure out a good easy way to avoid SQL Injection and so far I've only been able to come up with two ideas: Base64 encode the user input (Don't really want to do this) Use regex to remove unwanted characters. (Currently using this, not sure if it's 100% safe) Here is my current code: <?php $hash = $_GET['file']; if (isset($hash)) { $db = new SQLite3("Files.db"); if ($db != null) { $hash = preg_replace('/[^A-Za-z0-9 _.\-+=]/', '_', $hash); if ($response = $db->query("SELECT [FILE

filter words on a string in PHP

妖精的绣舞 提交于 2020-01-04 05:21:49
问题 I have a string in which all of the beginning of every word are capitalized. Now i want to filter it like if it can detect words link "as, the, of, in, etc" it will be converted to lower cases. I have a code that replace and convert it to lower case, but for 1 word only like below: $str = "This Is A Sample String Of Hello World"; $str = preg_replace('/\bOf\b/', 'of', $str); output: This Is A Sample String of Hello World So what i want is that to filter other words such as on the string like

Regex to join numbers when they have spaces between them

情到浓时终转凉″ 提交于 2020-01-03 20:48:07
问题 I'm trying to build a regex that joins numbers in a string when they have spaces between them, ex: $string = "I want to go home 8890 7463 and then go to 58639 6312 the cinema" The regex should output: "I want to go home 88907463 and then go to 586396312 the cinema" The regex can be either in python or php language. Thanks! 回答1: Use a look-ahead to see if the next block is a set of numbers and remove the trailing space. That way, it works for any number of sets (which I suspected you might

Regular expression, replace multiple slashes with only one

佐手、 提交于 2020-01-03 11:25:25
问题 It seems like an easy problem to solve, but It's not as easy as it seems. I have this string in PHP: ////%postname%/ This is a URL and I never want more than one slash in a row. I never want to remove the slashes completely. This is how it should look like: /%postname%/ Because the structure could look different I need a clever preg replace regexp, I think. It need to work with URLS like this: ////%postname%//mytest/test///testing which should be converted to this: /%postname%/mytest/test

Regular expression, replace multiple slashes with only one

耗尽温柔 提交于 2020-01-03 11:25:08
问题 It seems like an easy problem to solve, but It's not as easy as it seems. I have this string in PHP: ////%postname%/ This is a URL and I never want more than one slash in a row. I never want to remove the slashes completely. This is how it should look like: /%postname%/ Because the structure could look different I need a clever preg replace regexp, I think. It need to work with URLS like this: ////%postname%//mytest/test///testing which should be converted to this: /%postname%/mytest/test

php remove string: fa fa-phone famouse

被刻印的时光 ゝ 提交于 2020-01-03 05:21:07
问题 I want to remove the strings fa and fa-phone of the string: "fa famouse fa-phone". I tried: preg_replace('/fa.+$/','', 'fa famouse fa-phone'); But now it removes all strings with fa . 回答1: preg_replace('/fa[ \-](phone)?/','','fa famouse fa-phone'); Regex: https://regex101.com/r/bJ3nE7/1 fa (literal) [ \-] match a space or a hyphen (phone)? match the literal 'phone' 0 or 1 times 回答2: So i want to remove the string fa and all the strings a beginning with fa- But not other strings that contain

Regular expression matching A, B, and AB

↘锁芯ラ 提交于 2020-01-02 08:03:47
问题 I would like to create a regular expression that matches A , B , and AB , where A and B are quite complex regular expressions. One solution is to use (A|A?B) or (AB?|B) , but then I have to repeat one of the expressions. A?B? does not work, since this also matches the empty string. Is it possible to create this regular expression without repeating neither A nor B ? 回答1: Edit You may add a lookahead to require at least 1 char: (?=.)A?(?:B)? ^^^^ See the regex demo. If you need to support