Simple: How to Replace "All Between" with PHP

Simple: How to replace all between with php?

$search = "/[^<tag>](.*)[^<\/tag>]/";
$replace = "your new inner text";
$string = "<tag>i dont know what is here</tag>";
echo preg_replace($search,$replace,$string);

outputs:

<tag>your new inner text</tag>

PHP: Replace the content between two characters, in multiple occurrences

Your code from stackoverflow needs minor changes:

$search = "/(<tag>)(.*?)(<\/tag>)/";
$replace = '$1your new inner text$3';
$string = "<tag>i dont know what is here</tag> some text <tag>here's another one</tag>";
echo preg_replace($search,$replace,$string);

How to replace text that is only between two specific words

It was not easy to come up with a solution, but I think you can use something like this:

$string = 'hi !START hi hello there !END hello there';

function wordAndReplacement()
{
return [
'hi' => 'bye',
'hello there' => 'see you later',
'hello' => 'goodbye',
];
}

function returnSubstring($str)
{
$substring = trim(substr($str, strpos($str, '!START') + 6));
return trim(explode('!END', $substring)[0]);
}

function replace($str)
{
$substring = returnSubstring($str);

$newString = '';

foreach (wordAndReplacement() as $key => $value) {
if (strpos($substring, $key) !== false) {
!empty($newString) ? $newString = str_replace($key, $value, $newString) : $newString = str_replace($key, $value, $substring);
} elseif(strpos($substring, $value) !== false) {
!empty($newString) ? $newString = str_replace($value, $key, $newString) : $newString = str_replace($value, $key, $substring);
}
}

return $newString;
}

function joinStringAndReplacement($string)
{
$initial = explode('!START', $string)[0];
$final = substr($string, strpos($string, '!END') + 4);

return $initial . replace($string) . $final;
}

echo joinStringAndReplacement($string);

Firstly, the returnSubstring() method simply returns the string in between !START and !END.

Then, the wordAndReplacement() method returns an array with words you want to replace on the string.

Lastly, there is a loop through wordAndReplacement array, to replace every word of the array found in the string with its replacement (either on its key or its value), using str_replace().

Let me know if this helps.

PHP regex - replace all text between a tag

As you're already using the capture groups, why not actually use them.

$link = "<a href='some-dynamic-link'>Text to replace</a>";
$newText = "Replaced!";
$result = preg_replace('/(<a.*?>).*?(<\/a>)/', '$1'.$newText.'$2', $link);

Replacing text between two limts

You are defining $start and $end as arrays, but using it as normal variables. Try changing your code to this:

$start = '\[';
$end = '\]';
$msg = preg_replace('#('.$start.')(.*)('.$end.')#si', '$1 test $3', $row['body']);

PHP: Regex replace everything between to strings/HTML tags

The reason you're getting the error is because you've not escaped an opening [ character in your regular expression. Please see the [ I have marked below:

preg_replace('/\<p\>\[quote\]\<\/p\>[\s\S]+?\<p\>[\/quote\]\<\/p\>/', '', $string);
^

This has resulted in starting a character class that has not been closed. You should simply escape this opening brace like this:

preg_replace('/\<p\>\[quote\]\<\/p\>[\s\S]+?\<p\>\[\/quote\]\<\/p\>/', '', $string);

How to replace everything between {} [] () braces from a string?

This should work:

$name = "[hi] helloz [hello] (hi) {jhihi}";
echo preg_replace('/[\[{\(].*?[\]}\)]/' , '', $name);

Paste it somewhere like: http://writecodeonline.com/php/ to see it work.

How can i replace all specific strings in a long text which have dynamic number in between?

It seems you want to process the input in stages, to obtain all the numbers in specific lexical context first, and then modify the user input using some lookup technique.

The first step can be implemented as

preg_match_all('~\[_wc_acof_(\d+)]~', $text, $matches)

that extracts all sequences of one or more digit in between [_wc_acof_ and ] into Group 1 (you can access the values via $matches[1]).

Then, you may fill the $replacements array using these values.

Next, you can use

preg_replace_callback('~\[_wc_acof_(\d+)]~', function($m) use ($replacements){
return $replacements[$m[1]];
}, $text)

See the PHP demo:

<?php
$text = '<p>[_wc_acof_6] i want to convert this and it contains also this [_wc_acof_9] or can be this [_wc_acof_11] number can never be static</p>';

if (preg_match_all('~\[_wc_acof_(\d+)]~', $text, $matches)) {

foreach($matches[1] as $matched){
$replacements[$matched] = 'NEW_VALUE_FOR_'.$matched.'_KEY';
}
print_r($replacements);
echo preg_replace_callback('~\[_wc_acof_(\d+)]~', function($m) use ($replacements){
return $replacements[$m[1]];
}, $text);
}

Output:

Array
(
[0] => 6
[1] => 9
[2] => 11
)
NEW_VALUE_FOR_6_KEY i want to convert this and it contains also this NEW_VALUE_FOR_9_KEY or can be this NEW_VALUE_FOR_11_KEY number can never be static

Best way to replace/remove a specific character when it appears between 2 character sequences

So it was a good thing I asked for the output, because initially I had something else. Many people would use regular expressions here, but I often find those difficult to work with, so I took a more basic approach:

function extractWantedStuff($input)
{
$output = [];
$sections = explode('""', $input);
$changeThisSection = false;
foreach ($sections as $section) {
if ($changeThisSection) {
$section = str_replace(',', '', $section);
}
$output[] = $section;
$changeThisSection = !$changeThisSection;
}
return implode('""', $output);
}

$fullstring = ',""Word1, Word2"" and another thing ,""Word3, Word4""';

echo extractWantedStuff($fullstring);

The output would be:

,""Word1 Word2"" and another thing ,""Word3 Word4""

See: Example code

Slightly more optimized, by removing the $changeThisSection boolean:

function extractWantedStuff($input)
{
$output = [];
$sections = explode('""', $input);
foreach ($sections as $key => $section) {
if ($key % 2 != 0) { // is $key uneven?
$section = str_replace(',', '', $section);
}
$output[] = $section;
}
return implode('""', $output);
}

$fullstring = ',""Word1, Word2"" and another thing ,""Word3, Word4""';

echo extractWantedStuff($fullstring);

See: Example code

And further optimized, by removing the $output array:

function extractWantedStuff($string)
{
$sections = explode('""', $string);
foreach ($sections as $key => $section) {
if ($key % 2 != 0) {
$sections[$key] = str_replace(',', '', $section);
}
}
return implode('""', $sections);
}

$fullstring = ',""Word1, Word2"" and another thing ,""Word3, Word4""';

echo extractWantedStuff($fullstring);

See: Example code

Replace everything between and including two characters using regex in php

Regular expressions in PHP need to be delimited:

$message = preg_replace('/\[[^\]]*]/', '', $message);

Check out this documentation.

Also as a side note, you don't need to escape the closing ] if it is the first character in a character class:

$message = preg_replace('/\[[^]]*]/', '', $message);

(Whether that is more readable in this case is debatable. But it's good to know.)



Related Topics



Leave a reply



Submit