Avatar of swhitlow
swhitlow asked on

Match and Replace String with a certain pattern

Hi! I have the following string:
$click = 'onclick="confirm_receipt(\'[@::example_id::@]\');"';

I need to replace the [@::example_id::@]  with the following:
$value['example_id'];

So, that the final string will actually be:
$click = 'onclick="confirm_receipt(\'' . $value['example_id'] . ' \');";

I know I will need to use reg expressions but am unfamiliar with them right now and need to get this answer quickly.  :)

Also, I will not know the value in the middle of the string that has the surrounding [@:: ::@]

I have just used these characters to identify the variable that needs replaced. The [@:: and the ::@] characters can all go away. I have just used these as placemarkers.

So, the 'example_id' could very well be 'contact_id' or something else. I just need to extract that out of the string and create the new string with the $value['id']

Thanks!
PHP

Avatar of undefined
Last Comment
swhitlow

8/22/2022 - Mon
hielo

Try:
$click = preg_replace("/\[@::[^:]::@\]/",$value['example_id'], $click);
hielo

if [@::example_id::@] is always the same then you do not need a regular expression:
$click = str_replace("[@::example_id::@]",$value['example_id'],$click);
ASKER
swhitlow

Hielo - thanks for the quick feedback!

However, [@::example_id::@] will not always be the same. I will not know what the middle part will be.  It could be [@::contact_id::@]  or  [@::somethingelse_id::@]

I need to extract the middle id to put it into the $value[]
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
hielo

OK. then try my first suggestion
ASKER
swhitlow

I can't try your first suggestion since in the middle of the preg_replace it says "example_id".  I will not know that when creating this preg_replace.

you have:
$click = preg_replace("/\[@::[^:]::@\]/",$value['example_id'], $click);

this will not work as it is indicating example_id. Basically, I need to extract it first, then replace it. in pseudo code style it would read:

$click = 'onclick="confirm_receipt(\'[@::example_id::@]\');"';

$extracted_value = 'example_id';

$new_click = 'onclick="confirm_receipt(\'' . $value[$extracted_value] . ' \');";

I know how to do this with array splits, etc. However, I am trying to do this in one line of code with a preg_replace regular expression.
hielo

Perhaps this:
$click = 'onclick="confirm_receipt(\'[@::example_id::@]\');"';
$click = preg_replace("/(\[@::([^:]+)::@\])/",'${2}', $click);
echo $click;

Open in new window

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
hielo

Basically same as previous but only one set of parenthesis:
$click = 'onclick="confirm_receipt(\'[@::example_id::@]\');"';
$click = preg_replace("/\[@::([^:]+)::@\]/",'${1}', $click);
echo $click;
ASKER
swhitlow

Hielo - I tried the code. But all it did was get rid of the [@::  and the ::@]  and left me with this string:
onclick="confirm_sample_receipt('example_id');"

Remember, I need to be left with this:
onclick="confirm_sample_receipt($value['example_id']);"

Here is a good example. Imagine a database with the column example_id  = 123456.

Now, you have the string which is :
$click = 'onclick="confirm_receipt(\'[@::example_id::@]\');"';

example_id is the value I have to extract. So, Ultimately, I need to create a string that is this:
onclick="confirm_sample_receipt($value['123456']);"

Does this help at all?
hielo

>>Remember, I need to be left with this:
>>onclick="confirm_sample_receipt($value['example_id']);"
>>Does this help at all?
Not really. what you posted will send PHP code to the browser.
Your help has saved me hundreds of hours of internet surfing.
fblack61
ASKER
swhitlow

This is not sending PHP code to the browser. It is in the generation of the string I need to ultimately send the code to the browser. If I have a database value that I am returning in the field name of "example_id" and that value is '123456', then here would be the PHP code to construct the string:

$click = 'onclick="confirm_receipt(\'' . $value['example_id'] . '\');"';
echo $click;

The Result of the echo statement would be:
onclick="confirm_receipt('123456');"
ASKER
swhitlow

Hielo - I don't know if this will help or not but here is the code I am using currently to get the results I need. I am just looking to create cleaner looking code and reduce the amount of lines here. It will have differnet variables in this code sample. But, $customCode has the same string as $click. And this code does echo the right thing:  (please take note of the $value[$customValue] part on the last string)


$customCode = $this->simple_table_defs['add_columns_before']['customCode'];
$customCodeArray = explode('[@::', $customCode);
$customCodeArray2 = explode('::@]', $customCodeArray[1]);
$customValue = $customCodeArray2[0];
$customCode = $customCodeArray[0] . $value[$customValue] . $customCodeArray2[1];

Open in new window

ASKER CERTIFIED SOLUTION
hielo

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
swhitlow

Sorry - not understanding the above code. Where is $click or $matches defined?

Sorry about the confusion. Let's try this again with the code sample above. Let's define $customCode as our string. So, here is the string:

$customCode = 'onclick="confirm_receipt(\'[@::example_id::@]\');"';

Now, how would the above code work with this?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
swhitlow

Sorry! Nevermind.  I found out how to use it. It is like this:

$customCode = $this->simple_table_defs['add_columns_before']['customCode'];
preg_match("/\[@::([^:]+)::@\]/",$customCode,$matches);
$customCode = preg_replace("/\[@::([^:]+)::@\]/",$value[ $matches[1] ], $customCode);

No $click needed in there. :)

This works! Thank you very much!

Just for my own sanity - could you explain a little about what is happening with the preg_match and the preg_replace above? Thanks!
ASKER
swhitlow

Thank you very much for all your effort! I know this took a few times around to get right. I appreciate it!
hielo

>>No $click needed in there. :)
Oops.

using preg_match to "capture" the value between the pair of ::. The parenthesized match is placed in the $matches variable and found in the $match[1] array element.
you use that value in preg_replace to dereference your $value variable.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
ASKER
swhitlow

Real quick, how would you match multiple values of this same example? I know I am going to run into this pretty quickly. For instance:

sample_id is '123456';
transfer_id is '987654'

$customCode = 'onclick="confirm_sample_receipt(\'[@::sample_id::@]\', \'[@::transfer_id::@]\');"';

Now, there are two id's to capture. How would I modify the code above to loop through these two and give me the one long string of:
onclick="confirm_receipt('123456', '987654');"

Sorry about dragging this out. I just didn't want to open up a new question when this is the same thing only expanded a little. Let me know if I do need to open up another question for this one.

Thanks again!