PHP Use Regex match as key from POST array for replacement - php

I have these variables:
$summary="The problem with field2 is field13. The fix will be field7"
$_POST['field2']='thiscomputer';
$_POST['field7']='thishotfix';
$_POST['field13']='thisapplication';
I'm trying to craft a preg_replace() that will find /field[0-9]/ within the string and replace it with the value from the $_POST array. But I keep coming up short. Maybe preg_replace() is the wrong function to use in this instance.
I'm trying to replace an old long list of 50+ str_replace's
Thanks for any help that can point me in the right direction

How about this single line str_replace instead of preg_replace? You may need to fix some spacing, that can be resolved by the array value
echo str_replace(array_keys($_POST),array_values($_POST),$summary);
Output:
The problem with thiscomputer is thisapplication. The fix will be thishotfix
WORKING DEMO: https://3v4l.org/quYVO

strtr, though I'd probably want to copy and filter that post array:
<?php
$summary="The problem with field2 is field13. The fix will be field7";
$_POST['field2']='thiscomputer';
$_POST['field7']='thishotfix';
$_POST['field13']='thisapplication';
echo strtr($summary, $_POST);
Output:
The problem with thiscomputer is thisapplication. The fix will be thishotfix

Related

Adding characters to str_replace in PHP

I have a php file with thread ($tid) and post ($pid) ids defined and I'm looking to use str_replace to combine them and create my desired output (below) with &p= added:
Desired output:
$tid&p=$pid
The closest I've got is doing this:
$tid=$t1;
$pid=$p1;
$tid=str_replace("$tid","$tid"."$pid",$tid);
The result is:
$tid$pid
I may need to use a function (not sure which if so?) in the str_replace to support the &p= being added, as trying to add it in the quotes directly doesn't seem to work resulting only in database errors.
Edit #1: I tried doing the following based on the comments thus far:
$tid="";
$tid.="$t1";
$tid.="$p1";
$tid=$tid;
That results in the same as my previous example above:
$tid$pid
As soon as I add another with the &p= I get a database error:
$tid.="&p=";
So my question now is how to add the &p= to my Edit #1 example above correctly?
I'm attempting to understand the question a little bit. You have a couple options that are alluded to in the comments above. You could, for instance (where '1', '2', and '3' are replaced by the desired values):
<?php
$tid=1;
$pid=2;
$amp=3;
$tid=str_replace("$tid","$tid".'$amp;p='."$pid",$tid);
echo $tid;
?>
Here you can just explicitly state in the 'replace' argument of str_replace that the missing string should be part of the replacement string.
or you could also simply use the a concatenation operator to generate the string you desire which is probably simpler given what (little) I know of the surrounding code/use-case:
<?php
$tid=1;
$pid=2;
$amp=3;
echo "$tid".'$amp;p='."$pid";
?>
The reason I'd recommend the latter, is due to the fact that it's less resource intensive to actually produce the desired result which appears to just be a string. str_replace() searches the "haystack" or subject string using the search string and then must replace each of the found instances with the replace string.

How to get first part of a URL

can anyone help me to piece together the puzzled I'm facing. Lets say I have url's
/some-work/
/store/bread/alloy/
and in both of these cases I wanna fetch the first part from it. i.e. some-work, store.
Now I've used parse_url(get_permalink()) to get the array of the url and then fetch the path index of the array to fetch the above string. Now I have also checked strstr PHP function, but I am unable to make it work. Can anyone help?
You can use explode, array_filter and current function like as
$url = "http://www.example.com/some-work/";
$extracted = array_filter(explode("/",parse_url($url,PHP_URL_PATH)));
echo current($extracted);//some-work
Demo

Find and concatenate result in a pattern

I have a long PHP file and I want to copy all the variable names only and build an insert sql query. Is there a way where I can search for a pattern using regular expression and concatenate the find result till I collected all the variable and spit it out in a statement?
I am using TextMate and am familiar with regular expression search. Regex search result give $0,$1 and so forth argument. Do not know if this possible though. Solution in any editor will do not just text mate.
I have just too many variable (+100) don't feel like copy every single one. Here my sample file
$ID = $_POST['id'];
$TXN_TYPE = $_POST['txn_type'];
$CHARSET = $_POST['charset']
$CUSTOM = $_POST['custom'];
You could try something with get_defined_vars(). However this function also lists GLOBAL vars. You can use this snippet to remove them if you don't want them and display only the vars you defined
$variables = array_diff(get_defined_vars(), array(array()));
However this snippet generates Notices and I haven't found a way to solve them yet.
If you've only got $_POST variables you can loop through the $_POST array itself
You create the SQL programmatically while looping through the array.
My own solution is, do the inverse. It is not probably possible.
Leave only the variable names Remove all the rest. Use
[space].+ regex to remove everything that is after the variable name.
clean the file so that only variable names are left. then do a couple more find and replace to bring the variable name in the form you want.
If you're looking to match only the variable names (not the $_POST array indices), then the regular expression is pretty much provided in the PHP documentation:
\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*
This will, of course, include $_POST, but that should be easy enough to remove. If not, you could do it with negative lookahead (if TextMate supports it):
\$(?!_POST($|[^a-zA-Z0-9_\x7f-\xff]))[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*

first thing i do in my php function is define an array why is undefined later in the function?

I have a long function with many if statements and such. The first thing i do in my function is this: $text = file($read_text_file);
The $text array contain all text in the site therefor I need this array to be available in my entire function and I though I would accomplice this by defining the variable first thing in the function, but now i discovered that later in a if statement, its blank. Why is this?
Put var_dump($text); on the line immediately after $text = file($read_text_file);. If the array is populated, then you will get the values of the array. If not, then my guess is that there is a problem with the file itself.
As William said in the comments above we really need to see some context of this problem in order to answer this question. There could be any number of issues happening. Are you sure that the file is being read correctly as well?

some quick help with this small piece of code

How come when I echo $p, the variable which Im trying to fetch using this loop doesnt get displayed in the path.
$name_image2="picture.jpg";
for ($i=2; $i<=$nr_of_pics; $i++){
$img='name_image'.$i;
echo $$img; gives me this: 'picture.jpg' which is correct.
but when echoing $p like this:
$p="/SV/main/temp_images/$$img"; echo $p;
I get this: SV/main/temp_images/name_image2 --> the variable 'name_image2' doesnt get called here, why?
I want it to say: SV/main/temp_images/picture.jpg
Thanks
$p = "/SV/main/temp_images/" . $$img;
Ought to fix it.
Also, I would recommend learning how to use arrays. They are a much better way to have a set of data instead of variable variables.
Try $p="/SV/main/temp_images/{${$img}}";
When PHP is parsing the string and comes to a $, it looks at the next character to see if it makes a valid variable name. If not, it moves on. In this case, that means that the second $ is correctly interpreted, but the first one has already been passed by. The answer is to enclose the inner expression in brackets, so that it will be parsed before the outer one is.

Categories