-2

I want to find substring in a string with ___string_string___ pattern where string_string can be any string.

$string = "This string contains ___MY_VALUE___ which I want to replace and there is also ___ONE_MORE_VALUE___ which I want to replace";`
Tom
  • 4,257
  • 6
  • 33
  • 49
  • 2
    Okay, and what have you done in order to achieve this? – Epodax Feb 15 '17 at 09:46
  • It is not clear what your `string` part may contain. Please provide the requirements. An example code/regex showing what you tried would also help clearing that out. – Wiktor Stribiżew Feb 15 '17 at 09:48
  • I want to fetch strings with ___value___ pattern and then replace with other value. e.g: There will be string "This is my ___OTP___" then I will replace ___OTP___ with value like 1234, but there can be multiple __OTP1__,__OTP2__ in a string and I wanted to replace all with different values. – Ganesh Sawant Feb 15 '17 at 09:49
  • 1
    show the replacement values – RomanPerekhrest Feb 15 '17 at 09:50
  • 1
    Possible duplicate of [How to check if one string contains another substring in JavaScript?](http://stackoverflow.com/questions/1789945/how-to-check-if-one-string-contains-another-substring-in-javascript) – Kindle Q Feb 15 '17 at 09:51
  • Possible duplicate of [How do I check if a string contains a specific word in PHP?](http://stackoverflow.com/questions/4366730/how-do-i-check-if-a-string-contains-a-specific-word-in-php) – pringi Feb 15 '17 at 10:06

4 Answers4

1

Provided you simply want a string & underscores, and nothing else, in between the ___'s, then;

/___([a-zA-Z_]*)___/m

Here's the working example

Edit

To fix the false positive on ______, I've added a positive lookahead, and made a couple of other tweaks.

/_{3}(?=.*[a-zA-Z_])(.*[a-zA-Z_])_{3}/m

_{3} - Matches 3 underscores

(?=.*[a-zA-Z_]) - Positive lookahead to make sure one of these characters is present

(.*[a-zA-Z_]) - The actual matching group

_{3} - And match the ending 3 underscores

Here's a working example of this second version

Tom
  • 4,257
  • 6
  • 33
  • 49
1

Try this using preg_replace_callback():

$string = "This string contains ___MY_VALUE___ which I want to replace and there is also ___ONE_MORE_VALUE___ which I want to replace";

$replaced = preg_replace_callback("/\___([a-zA-Z_]*)\___/", function($m){
    return "(replaced {$m[1]})";
}, $string);


print_r($replaced);
mith
  • 1,680
  • 1
  • 10
  • 12
1

The solution using preg_replace_callback with specific regex pattern:

// custom replacement list
$pairs = ['___MY_VALUE___' => 123, '___ONE_MORE_VALUE___' => 456];
$str = '"This string contains ___MY_VALUE___ which I want to replace and there is also ___ONE_MORE_VALUE___ which I want to replace";';

$str = preg_replace_callback('/___(([a-z]+_?)+)___/mi', function($m) use($pairs){
    return (isset($pairs[$m[0]]))? $pairs[$m[0]] : $m[0];
}, $str);

print_r($str);

The output:

"This string contains 123 which I want to replace and there is also 456 which I want to replace";
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
0

To replace everything that is not 3 underscores in between two "3 underscores", use this regex:

/___((?:(?!___).)+)___/

To replace "keywords" with corresponding values, use preg_replace_callback like that:

$replace = array(
'MY_VALUE' => '**Replacement of MY_VALUE**',
'ONE_MORE_VALUE' => '**Replacement of ONE_MORE_VALUE**',
' third value to be replaced ' => '**Replacement of  third value to be replaced**',
);

$string = "This string contains ___MY_VALUE___ which I want to replace 
    and there is also ___ONE_MORE_VALUE___ which I want to replace. 
    This is the ___ third value to be replaced ___";

$string = preg_replace_callback('/___((?:(?!___).)+)___/', 
            function ($m) use($replace){
                return $replace[$m[1]];
            }
            , $string);
echo $string,"\n";

Output:

This string contains **Replacement of MY_VALUE** which I want to replace 
and there is also **Replacement of ONE_MORE_VALUE** which I want to replace. 
This is the **Replacement of  third value to be replaced**
Toto
  • 89,455
  • 62
  • 89
  • 125