String Manipulation Question

Hey,

What is the best function to use when trying to remove dynamic data, from a string, that consists of one pattern?

Example:

$content="This is a test paragraph. In this paragraph I type words. I also have the following dynamic data as listed below:

[bit1_bit2_bit3] - [otherbit1_otherbit2_otherbit3]";

I want to take content and remove the
“[bit1_bit2_bit3] - [otherbit1_otherbit2_otherbit3]”

automatically without knowing what values exist for bit1, bit 2…otherbit1, etc…

The dynamic data is basically 2 sets of brackets, separated by ‘-’ and the data inside the brackets (i.e bit1_bit2_bit3) are dynamic. They can be any integer at any point.

I basically want to get my wordpress function to strip that out before it publishes but I am not sure which string function is ideal, especially for performance, for taking anything between , without being able to pinpoint exactly what value is between those brackets. If I knew the values were hardcoded, I could create a str_replace to remove them so it doesn’t appear in the post. That’s easy.

I just stink at telling it "Regardless of what is between these brackets, for both sets of data in brackets, replace them with “”

Should I be using REGEX?

Thanks

PS. In case you are curious why this is important, the tokens are useful for performing functions but I don’t want those tokens to appear to the user when the post loads. Since the data is dynamic, I can’t use a hard coded str_replace to remove them manually. That’s all.

Thanks, again.

The tokens, when I view the source code, appear like this:
<p>[left_21_4] – [right_20_2]</p>

Could those extra chars that I am not taking into consideration be the issue? Instead of looking for the
hyphen, maybe I use the special chars as what is used in preg_replace?

EDIT. While it doesn’t show HERE, I don’t see the ‘-’ in the source code, only the special chars equivalent. Same with the paragraph and close paragraph tags before and after the tokens.

EDIT #2: YES, THAT WAS IT! It needed to place the special chars, not the literal ‘-’, for it to work. Thanks, again.

Hmm. Should work.

Maybe echo the string BEFORE you do the preg_replace, to make sure it actually does contain valid data?

And neither does it answer the question asked. :wink:

That regex example is neat and efficient. :slight_smile:

May be better solution:

function replaceVariables($string, $arrayOfVariables){
    foreach($arrayOfVariables as $k => $v) $$k = $v;
    
    eval("\\$string = \\"$string\\";");

    return $string;
}

$developers = array('a' => 'John', 'b' => 'Peter', 'c' => 'Jake');
$string = '$a, $b and $c are great PHP Developers!';

echo replaceVariables($string, $developers);

Regards,
Jake

eval? There is no need whatsoever. That solution is not better.


<?php
$string = '
This is a test paragraph. In this paragraph I type words. I also have the following dynamic data as listed below:

[bit1_bit2_bit3] - [otherbit1_otherbit2_otherbit3]

This is a test paragraph. In this paragraph I type words. I also have the following dynamic data as listed below:

[bit1_bit2_bit3] - [otherbit1_otherbit2_otherbit3]
';

echo preg_replace(
   '~(\\[[^\\]]+]\\s-\\s\\[[^\\]]+])~',
   null,
   $string
);

/*
    This is a test paragraph. In this paragraph I type words. I also have the following dynamic data as listed below:
    
    
    This is a test paragraph. In this paragraph I type words. I also have the following dynamic data as listed below:


*/
?>

:wink:

Hey, the function and logic are great and work with a string when echoing. What’s strange for me is when I try to create it as a var (using $content, which is what contains the wordpress post) and return that, I don’t see the replace function work.

Imagine $content is the wordpress post having those tokens as shown above.
If I do $content=preg_replace function…

nothing happens.

EDIT:

I can use it as a variable if I hard code the content of the variable, such as ‘$content’ above.
But wordpress is not allowing it when I use the filter. Weird…I have used a bunch of str_replaces before
but nothing that should affect it.

To all of you, thanks. Regarding the solution, preg_replace will be where I start to fully grasp how this works (Clearly I need to get over my hurdles with regex)

Should anything wonky happen I will update this thread. Otherwise, I look forward to implementing this and thank you for your time. I’m coming to appreciate just how critical mastering all string/regex functions is.