i thinking how can I replacing last item of form names like this:
content[1]
content[1][officeImage]
content[2][officeImage]
content[3][something][officeImage]
...
Depth of "array" can be infinity and I need replace last of him like this:
content[replaced]
content[1][replaced]
content[2][replaced]
content[3][something][replaced]
...
Is here someone who can write preg_replace() or etc?
Thanks
Edit:
I have this:
preg_replace('~\[.*!?\]~', '[replaced]', $parent);
But it return from "content[1][officeImage]" this "content[replaced]" :[
Try this:
preg_replace('~^.*\[\K[^]]+~m', 'replaced', $parent);
The m (multi-line) modifier is only necessary if $parent contains multiple lines needing to be replaced at once.
The big trick here is starting with ^.*\[\K. This says match everything starting with the beginning of the line up to a \[. Since this is a "greedy" repetition it will keep going until the last instance (. doesn't match newlines without the s modifier). The \K throws away everything matched so we only are replacing what's inside the brackets.
You can use this code:
$re = "/\\[([^]]*)\\][^]]*$/m";
$str = "content[1]\ncontent[1][officeImage]\ncontent[2][officeImage]\ncontent[3][something][officeImage]";
$subst = "[replacement]";
$result = preg_replace($re, $subst, $str);
See it live PHP or live Regex
Try this:
$string = 'content[3][something][officeImage]';
$replaced = 'somestring';
$result = preg_replace('/(.*\[)(.*?)]$/m','$1'.$replaced.']',$string);
Related
Sorry for my bad English in Advance. Here is my JSON return.
https://images-na.ssl-images-amazon.com/images/M/MV5BYzc3OGZjYWQtZGFkMy00YTNlLWE5NDYtMTRkNTNjODc2MjllXkEyXkFqcGdeQXVyNjExODE1MDc#._V1_UY268_CR5,0,182,268_AL_.jpg
How can I remove the part UY268_CR5,0,182,268_AL_. Specifically that part only. And I have many of this links. Each having different strings there. For example:
https://m.media-amazon.com/images/M/MV5BYWNlMWMxOWYtZWI0Mi00ZTg0LWEwZTMtZTEzZDY0NzAxYTA4XkEyXkFqcGdeQXVyMTQxNzMzNDI#._V1_UX182_CR0,0,182,268_AL_.jpg
As shown it is different. I want to remove the part UX182_CR0,0,182,268_AL_. Each of the results I have has almost the same structure but the end part I want to remove. I am on laravel and so I am encoding my jsons result from controller. Is there anyone this can be done with php?
Update:
Here is the code I tried.
$json = json_decode($data,true);
$slice = str_replace("UY268_CR5,0,182,268_AL_","", $json);
return $slice ['poster'];
The string is removed but what about different strings with different URL's like mentioned above?
You can try with preg_replace() with the combination of lookahead and lookbehind
<?php
$re = '/(?<=_V1_)(.+?)(?=.jpg)/';
$str = 'https://m.media-amazon.com/images/M/MV5BYWNlMWMxOWYtZWI0Mi00ZTg0LWEwZTMtZTEzZDY0NzAxYTA4XkEyXkFqcGdeQXVyMTQxNzMzNDI#._V1_UX182_CR0,0,182,268_AL_.jpg';
$subst = '';
$result = preg_replace($re, $subst, $str, 1);
echo "The result of the substitution is ".$result;
?>
DEMO: https://eval.in/1044470
REFF: Regex lookahead, lookbehind and atomic groups
REGEX EXPLANATION: https://regex101.com/r/aHAw5f/1
I am trying to search this coincidence in a string:
1. I need to take only numbers after the chracter '#' as long as this coincidence has not spaces, for example:
String = 'This is a test #VVC345RR, text, and more text 12345';
I want to take only this from my string -> 345.
My example:
$s = '\"access_token=103782364732640461|2. myemail#domain1.com ZmElnDTiZlkgXbT8e3 #DD234 4Jrw__.3600.1281891600-10000186237005';
$matches = array();
$s = preg_match('/#([0-9]+)/', $s, $matches);
print_r($matches);
This only works when I have one # and numbers.
Thanks!
Maybe:
#\D*\K(\d+)
Accomplishes what you want?
This will look for an #, any non-numbers, and then capture the numbers. The \K ignores the early match.
https://regex101.com/r/gNTccx/1/
I'm unclear what you mean by has not spaces, there are no spaces in the example string.
For example, if I want to get rid of the repeating numeric suffix from the end of an expression like this:
some_text_here_1
Or like this:
some_text_here_1_5
and I want finally receive something like this:
some_text_here
What's the best and flexible solution?
$newString = preg_replace("/_?\d+$/","",$oldString);
It is using regex to match an optional underscore (_?) followed by one or more digits (\d+), but only if they are the last characters in the string ($) and replacing them with the empty string.
To capture unlimited _ numbers, just wrap the whole regex (except the $) in a capture group and put a + after it:
$newString = preg_replace("/(_?\d+)+$/","",$oldString);
If you only want to remove a numberic suffix if it is after an underscore (e.g. you want some_text_here14 to not be changed, but some_text_here_14 to be changed), then it should be:
$newString = preg_replace("/(_\d+)+$/","",$oldString);
Updated to fix more than one suffix
Strrpos is far better than regex on such a simple string problem.
$str = "some_text_here_13_15";
While(is_numeric(substr($str, strrpos($str, "_")+1))){
$str = substr($str,0 , strrpos($str, "_"));
}
Echo $str;
Strrpos finds the last "_" in str and if it's numeric remove it.
https://3v4l.org/OTdb9
Just to give you an idea of what I mean with regex not being a good solution on this here is the performance.
Regex:
https://3v4l.org/Tu8o2/perf#output
0.027 seconds for 100 runs.
My code with added numeric check:
https://3v4l.org/dkAqA/perf#output
0.003 seconds for 100 runs.
This new code performs even better than before oddly enough, regex is very slow. Trust me on that
You be the judge on what is best.
First you'll want to do a preg_replace() in order to remove all digits by using the regex /\d+/. Then you'll also want to trim any underscores from the right using rtrim(), providing _ as the second parameter.
I've combined the two in the following example:
$string = "some_text_here_1";
echo rtrim(preg_replace('/\d+/', '', $string), '_'); // some_text_here
I've also created an example of this at 3v4l here.
Hope this helps! :)
$reg = '#_\d+$#';
$replace = '';
echo preg_replace($reg, $replace, $string);
This would do
abc_def_ghi_123 > abc_def_ghi
abc_def_1 > abc_def
abc_def_ghi > abc_def_ghi
abd_def_ > abc_def_
abc_123_def > abd_123_def
in case of abd_def_123_345 > abc_def
one could change the line
$reg = '#(?:_\d+)+$#';
I have a query string that may look like one of the following:
?key=aa=bb
?key=aa=bb=cc
?key=aa=bb&key2=cc
etc.
What I want to do is replace the equal sign in the value part only. So it should result in this:
?key=aa%3dbb
?key=aa%3dbb%3dcc
?key=aa%3dbb&key2=cc
I'm trying to do that with the following regex by using a look ahead. But it's not doing anything.
echo preg_replace("/=(?=[^&])=/", "%3d", 'http://www.example.com?key=aaa=bbb=ccc&key3=dddd');
Example code here
How can I make this work?
(\bkey\d*)=(*SKIP)(*F)|=
Try this.See demo.
https://regex101.com/r/hR7tH4/13
$re = "/(\\bkey\\d*)=(*SKIP)(*F)|=/m";
$str = "\n ?key=aa=bb\n ?key=aa=bb=cc\n ?key=aa=bb&key2=cc\n";
$subst = "%3d";
$result = preg_replace($re, $subst, $str);
You don't need regex, use the proper tools. parse_url() to get the query string (and whatever else you want), then parse_str() to get an array of the var/vals. Then http_build_query() will encode for you:
$query = parse_url('http://www.example.com?key=aaa=bbb=ccc&key3=dddd', PHP_URL_QUERY);
parse_str($query, $array);
$result = http_build_query($array);
Here is another version of a regex based on the same approach as vks':
[&?][^&=]+=(*SKIP)(*FAIL)|=
Regex explanation:
[&?] - Match & or ? literally
[^&=]+ - Match characters other than & and =
= - Match = (so, we matched a key)
(*SKIP)(*FAIL) - Verbs that fail the match at this point (we do not replace this = we found after key)
= - We match any other = and we'll remove it.
Here is IDEONE demo:
$re = "/[&?][^&=]+=(*SKIP)(*FAIL)|=/";
$str = "http://google.com?key=aa=bb\nhttp://google.com?key=aa=bb=cc\nhttp://google.com?key=aa=bb&key2=cc";
$result = preg_replace($re, "%3d", $str);
echo $result;
What about this?
preg_replace_callback("/=([^&$]+)/", "myReplace", "http://www.example.com?key=aaa=bbb=ccc&key3=dddd");
function myReplace($matches) {
return "=" . urlencode($matches[1]);
}
Just gonna add an addendum, explaining your specific attempt:
preg_replace("/=(?=[^&])=/",
↑ ↑
While the lookahead was a nice idea, it really just would match a single character. And in this case just would have asserted the very next character not to be &.
You could refashion it into:
preg_replace("/=([^&=]+)\K=/",
↑
Which I guess is what you tried. Note that this merely ignores every second =………= equal sign. So would only suit your simple example query strings, not more plentiful unescaped characters within.
I have a fragment in text like
;flat
CID_999999 = 99999999
CID_999999 = 99999999
CID_999999 = 99999999
CID_999999 = 99999999
where 999999 are some numbers. I want to find this fragment by regexp.
When I use this regex:
preg_replace('/;flat[\s\r\n]+[CID_0-9]+\s=\s[0-9]+\n/','REPLACEMENT',$content);
it replaces ;flat and first CID string.
So, I suppose that if I put [CID_0-9]+\s=\s[0-9]+\n part in character class []+it will remove all CID strings. But if I do it it doesn't replace anything. So I don't understand something in regex. What am I doing wrong?
I thought that expected output is clear, but ok. I should replace all fragment by REPLACEMENT.
Use lookbehind if you are trying to match just the digits after the = and before the newline
For more accurate matching use CID[0-9]+ rather than [CID0-9]+
preg_replace('/(?<;flat[\s\r\n]+CID_[0-9]+\s=\s)[0-9]+(?=\n)/','REPLACEMENT',$content);
You don't need a character class ([...]); you need a group ((...)). Your code should look like this:
preg_replace('/;flat[\s\r\n]+([CID_0-9]+\s=\s[0-9]+\n?)+/', 'REPLACEMENT', $content);
Note the ? at the end, just in case your last line isn't terminated with a new line (\n) character.
Demo
;flat\s*|(?!^)\G(CID_\d+\s*=\s*\d+\s*)
Try this.Replace by $1.See demo.
https://regex101.com/r/gQ3kS4/7
$re = "/;flat\\s*|(?!^)\\G(CID_\\d+\\s*=\\s*\\d+\\s*)/";
$str = ";flat\nCID_999999 = 99999999\nCID_999999 = 99999999\nCID_999999 = 99999999\nCID_999999 = 99999999\n;";
$subst = "$1";
$result = preg_replace($re, $subst, $str);