Preg_Replace for a big int fitting a certain pattern - php

Within a GIGANTIC string I am trying to clean into json. I encounter something like this that breaks the script (usually multiple times from the same source)
{
29646191: [bunchofjson]
}
is there a preg_replace I can do to replace all occurances of
{
stringofrandomlysizednumbers: [anything]
}
With
{
"string...numbers": [anything]
}
List of things that havent worked:
$output = preg_replace('/([^\\\])":([0-9]{10,})(,|})/', '$1":"$2"$3', $output);
$output = preg_replace('/("\w+"):(\d+)(.\d+)?/', '\\1:"\\2\\3"', $output);
$output = preg_replace('/("\w+"):(\d+)/', '\\1:"\\2"', $output);
$output = preg_replace('/(\d+):/', '"$1":', $output);
$output = preg_replace('/("\w+"):(\d+)(.\d+)?/', '\\1:"\\2\\3"', $output);
$output = preg_replace('/:\s*(\-?\d+(\.\d+)?([e|E][\-|\+]\d+)?)/', ': "$1"', $output);
$output = json_decode($output, true, 512, JSON_BIGINT_AS_STRING));
(I json decode into an associative array when done in an ideal case)

I am guessing, and have not checked my code, but try this.
$output = preg_replace('/^\s*[0-9]{10,}.*/', '"string...numbers": \[anything\]', $output);
(I'm not sure the \[ and \] are required. I don't think they are but I put them there just encase...
the preg_replace will replace any line that has any number of spaces, then 10 consecutive numberic characters.
Input is exactly like this?
{
29646191: [bunchofjson]
}
Output will be like this
{
"string...numbers": [anything]
}

I believe you are looking for this replacement:
$output = preg_replace('/(?<=\s)(\d+)(?=\s*:)/', '"$1"', $output);

Related

How to preg_match() a string and output all results into an array?

I want to output all matching results into an array.
I'm only able to output one word and not both.
$input = "I like to eat <cookies> with <coke>.";
$output = [];
preg_match('~<(.*?)>~', $input, $output);
echo $output[1];
It should result with something like:
$output = ["cookies", "coke"];
You were really close. Use preg_match_all() instead.
$input = "I like to eat <cookies> with <coke>.";
$output = [];
preg_match_all('~<(.*?)>~', $input, $output);
print_r($output[1]);

Clean url string with backslashes and brackets using php so it is a useable url

I'm returning an array from gravity forms and the value contains a url, but for some reason it's added brackets, quote marks, and backslashes!?
I've looked at preg_replace() but seems a little long winded.
And I've look stripslashes() but it removes all the slashes.
My question is there a simple function solution that makes this string value into a usuable url which I can echo out in my page?
Here is my string value below in its entirety...
["http:\/\/joshbakerson.com\/wp-content\/uploads\/gravity_forms\/1-61ecbcd1ce76f3a9c22cc8ee3d541e5b\/2017\/01\/The-Funniest-moment-ever-when-bear-starts-then-bottles-it.mp4"]
I simply want to convert the string above to this...
http://joshbakerson.com/wp-content/uploads/gravity_forms/1-61ecbcd1ce76f3a9c22cc8ee3d541e5b/2017/01/The-Funniest-moment-ever-when-bear-starts-then-bottles-it.mp4
Any advice on what function I should use would be great thanks.
stripslashes() should work for you, as it only removes the \ characters:
$x = '["http:\/\/joshbakerson.com\/wp-content\/uploads\/gravity_forms\/1-61ecbcd1ce76f3a9c22cc8ee3d541e5b\/2017\/01\/The-Funniest-moment-ever-when-bear-starts-then-bottles-it.mp4"]';
$output = stripslashes($x);
you can then use str_replace() to remove the [, ], and " characters:
$output = str_replace('[','', $output);
$output = str_replace(']','', $output);
$output = str_replace('"','', $output);
echo $output;
// http://joshbakerson.com/wp-content/uploads/gravity_forms/1-61ecbcd1ce76f3a9c22cc8ee3d541e5b/2017/01/The-Funniest-moment-ever-when-bear-starts-then-bottles-it.mp4
Try this:
<?php
$string = 'http:\/\/joshbakerson.com\/wp-content\/uploads\/gravity_forms\/1-61ecbcd1ce76f3a9c22cc8ee3d541e5b\/2017\/01\/The-Funniest-moment-ever-when-bear-starts-then-bottles-it.mp4';
$data = str_replace('\/','\\',$string);
echo $data;
?>

Encode contents between Two Special Strings

All I want is to Get contents between two strings like the following line:
$content = '81Lhello82R 81Lmy82R 81Lwife82R';
I wish to get all contents between 81L and 82R, then encode them to Base64 automatically by Preg_match I think, I've done some ways to do it but didn't get what was expected!
Base Form:
81Lhello82R 81Lmy82R 81Lwife82R
Output:
81LaGVsbG8=82R 81LbXk=82R 81Ld2lmZQ==82R
Hard rules:
$leftMask = '81L';
$rightMask = '82R';
$content = '81Lhello82R 81Lmy82R 81Lwife82R';
preg_match_all('#'.$leftMask.'(.*)'.$rightMask.'#U',$content, $out);
$output = [];
foreach($out[1] as $val){
$output[] = $leftMask.base64_encode($val).$rightMask;
}
$result = str_replace($out[0], $output, $content);
RegExp rules
$leftMask = '\d{2}L';
$rightMask = '\d{2}R';
$content = '81Lhello82R 81Lmy82R 81Lwife82R';
preg_match_all('#('.$leftMask.')(.*)('.$rightMask.')#U',$content, $out);;
$output = [];
foreach($out[2] as $key=>$val){
$output[] = $out[1][$key].base64_encode($val).$out[3][$key];
}
$result = str_replace($out[0], $output, $content);
This is a job for preg_replace_callback:
$content = '81Lhello82R 81Lmy82R 81Lwife82R';
$output = preg_replace_callback(
'/(?<=\b\d\dL)(.+?)(?=\d\dR)/',
function($matches) {
return base64_encode($matches[1]); // encode the word and return it
},
$content);
echo $output,"\n";
Where
(?<=\b\d\dL) is a positive lookbehind that makes sure we have 2 digits and the letter L before the word to encode
(?=\d\dR) is a positive lookahead that makes sure we have 2 digits and the letter R after the word to encode
(.+?) is the capture group that contains the word to encode
Output:
81LaGVsbG8=82R 81LbXk=82R 81Ld2lmZQ==82R

regex php special characters

$word = file_get_contents('http://www.pixelmon-server-list.com/list.txt');
$content = file_get_contents('http://www.pixelmon-server-list.com/fleetyfleet.txt');
$found_dimensions = array(); // our array
$word_array = explode(' ', $word); // explode the list
foreach($word_array as $one_word) { // loop over it
$str = 'DimensionName'.$one_word; // what are we looking for?
if(strstr($content, $str) !== false) { // look for it!
echo $one_word; // Just for demonstration purposes
$found_dimensions[] = $one_word; // add to the array
}
}
okay i have a list.text and a fleetyfleet.txt
both can be viewed here i didn't post them for space sake
http://pastebin.com/7hWDUG1b
but what i want to do is find the words in list.txt but only add them to array if there prefix is Dimension�����Name� but the special characters make it kinda tough I'm not sure what i should do
I had a similar problem where I needed to remove all non-ascii characters from a file. Here's the regex I used:
s/[^\x00-\x7F]//g
If you're on linux, here's a quick one-liner:
perl -p -i -e "s/[^\x00-\x7F]//g" list.txt
Having to guess a little bit as I can't see the files from behind my firewall. The following code might help you:
<?php
$f1 = explode("\n",file_get_contents("./file1.txt"));
$f2 = file_get_contents("./file2.txt");
$found = array();
foreach($f1 as $x) {
$str = "/DimensionName.....$x./";
if (strlen($x)>0) {
if (preg_match($str, $f2, $matches)) {
echo $matches[0]."\n";
}
}
}
?>
This prints out lines that include a pattern DimensionName followed by 5 "anything" followed by whatever word was read from the first file file1.txt.
If you need this to be further refined, please leave a comment.

How to explode a string with commas and recompose except the first one

I have a string like this [tubelist dijfisj, ijdsifjad, ajkdfksd, sdjfkdf] and I would like to separate them into two ###URL### and ###URL2###.
This is the code I got so far
function xyz_plugin_callback($match)
{
$tag_parts = explode(",", rtrim($match[0], "]"));
$output = YOUX_TARGET;
$output = str_replace("###URL###", $tag_parts[1], $output);
$output = str_replace("###URL2###", $tag_parts[2], $output);
}
$match is the variable that I'm passing in.
You can utilize regular expressions here.
So if you do something like:
$temp_match = trim($match[0], "[]");
$urls = array();
preg_match("/([\w\s]*),([\w\s]*),.*/", $temp_match, $urls);
$url1 = $urls[1];
$url2 = $urls[2];
// do your $output str_replace here.
finally, I got it working! Here is the working code.
$tag_parts1 = explode(" ", rtrim($match[0], "]"));
$tag_parts = explode(",",$tag_parts1[1],2);
the first line will strip the [tubelist and ].
the second line with store the first set of value in array[0] and the rest to [1].
voila, case resolved.

Categories