Using preg_replace to replace all occurrences in php

Regex is absolutely my weak point, and I have all completely stalled. I am creating a fairly basic search function and I need to change my user input based on the following template:

Subject:

%22first set%22 %22second set%22-drupal -wordpress

Required Conclusion:

+"first set" +"second set" -drupal -wordpress

I'm sorry that I can’t help, because I usually like to at least post the solution that I have, but I'm at a loss for that.

Any help is appreciated. Thank.

+3
source share
3 answers
preg_replace('/%22((?:[^%]|%[^2]|%2[^2])*)%22/', '+"$1"', $str);

: $1 - , () , ((?:[^%]|%[^2]|%2[^2])*). [^%] (...|...|...) , %22 - . . http://en.wikipedia.org/wiki/Regular_expression#Lazy_quantification.

JavaCC (/* */), -, , : 12345 12345........12345 12345 : /12345([^1]|1[^2]|12[^3]|123[^4]|1234[^5])*12345/

+1

, URL. urldecode,

"first set" "second set" -drupal -wordpress

( , -drupal).

+. , , , - :

$str = '"first set" "second set" -drupal -wordpress foo';
echo preg_replace('#( |^)(?!(?:\w+"|-| ))#','\1+', $str));
// prints +"first set" +"second set" -drupal -wordpress +foo

: urldecode, str_replace %22 ".

+2

Is this what you are looking for?

<?php
  $input = "%22first set%22 %22second set%22-drupal -wordpress";
  $res = preg_replace( "/\%22(.+?)\%22/","+\"(\\1)\" ", $input);
  print $res;
?>
+1
source

Source: https://habr.com/ru/post/1784453/


All Articles