I have string :
$string = 'ABCCDF[GH]IJJ[KLM]';
How to i get string 'GH' and 'KLM' in php
I think i will use preg_split, but I know not more about reg. Plz help me.
Try this;
>>> $string = 'ABCCDF[GH]IJJ[KLM]';
=> "ABCCDF[GH]IJJ[KLM]"
>>> preg_match_all('/\[(\w+)\]/', $string, $matches);
=> 2
$matches will look like below.
[
[
"[GH]",
"[KLM]",
],
[
"GH",
"KLM",
],
]
Related
This is my input content:
$input = 'Hi there,
You recently ... {{client.events.add_to_cart.product_name}}, ...
..
Also
...
{{client.events.purchase.product_name}}
';
and this is regex example:
$regex = "'(?:\{\{client\.events\.)\w+(?:\.)\w+(?:\}\})'";
preg_match_all($regex, $input, $matches);
This is the $matches content:
array:1 [
0 => array:2 [
0 => "{{client.events.add_to_cart.product_name}}"
1 => "{{client.events.purchase.product_name}}"
]
]
Not bad, but I'd like somehow to pick "add_to_cart" and "product_name" as well. I can do it with explode() function, but wonder if there is a way to do this by regex only once.
Also, I wonder if it is possible to have 'target' these too:
{{client.events.add_to_cart.product_name}}
{{client.events.add_to_cart.0.product_name}}
{{client.events.add_to_cart.1.product_name}}
with and without numbers. And also knowing the number.
Thanks.
This will do it: (\{\{client\.events\.(?:purchase|(add_to_cart))(?:\.\d)?\.(.*)\}\})
See: https://regex101.com/r/EKAJsG/1
My question is a bit different to most like this, I basically want to do the opposite to this question from Haluk.
So I have a JSON string:
{
"main":
{
"title": "QuickPub",
"defaultRole": "BU"
},
"roles":
{
"TU":
{
"name": "testUser",
"code": "TU"
}
}
}
and I want to be able to generate a string containing a php array definition from it:
<?php
return [
"main" =>
[
"title" => "QuickPub",
"defaultRole" => "BU"
],
"roles" =>
[
"TU" =>
[
"name" => "testUser",
"code" => "TU"
]
]
];
?>
EDIT:
I have tried json_decode() but this produces a variable, I need a string that I can put in a php file that will reproduce this without using php_decode.
I think this will solve your problem. First of all convert your json string to an array using json_decode($string,1); then convert that array to string representation using print_r($array,1); this will return your result as array string representation.
For example:
$json='{}' // its a demo
$array= json_decode($json,1); // an array
$result = print_r($array,1);
echo $result;
This is an adaptation of Being Sunny's answer, but using the var_export() function rather than print_r.
As described here by phihad
var_export prints valid php code. Useful if you calculated some values and want the results as a constant in another script
the script:
$json = '{"main":{"title": "QuickPub","defaultRole": "BU"},"roles":{"TU":{"name": "testUser","code": "TU"}}}';
$array = json_decode($json, 1);
$result = var_export($array, 1);
echo $result;
produces:
array(
'main' => array(
'title' => 'QuickPub',
'defaultRole' => 'BU',
),
'roles' => array(
'TU' => array(
'name' => 'testUser',
'code' => 'TU',
),
),
)
This can be achieved using this code:
$output = 'return ' . rtrim(preg_replace(['/{/', '/}/', '/:/'], ['[', ']', ' =>'], $json)) . ';';
this replaces { with [, } with ], and : with =>, trims any whitespace from the end, appends a ; and prepends a return statement.
this produces the output requested in the question bar the open and closing php tags.
i got an array
$array = [
"a" => "c",
"b" => "d",
"c" => "a",
"d" => "b",
];
and a string $text = "dcab";
How can i replace with my array elements each letter on my string, im try to figure out by steps but not luck,
1.- explode my string
2.- for each letter str_replace,
output will be: bacd
thank for help.
Well you can't use str_replace() because of the big warning that it shows in the documentation:
Replacement order gotcha
Because str_replace() replaces left to right, it might replace a previously inserted value when doing multiple replacements. See also the examples in this document.
Then take a look at the strtr() function
echo strtr($text, $array);
I'm trying to generate a JSON object using PHP to be passed to the client using data that is already stored in MySQL database.
The database contains a list of multipolygon coordinates like below in a single text field:
[
[
[
[
104.39209000000005,
-4.850154
],
[
108.17138687500005,
-3.721745195827911
],
[
112.12646500000005,
-1.274309
],
[
103.02978499999995,
-3.579213
]
]
]
]
When I try to generate a JSON object using json_encode I get the following:
{
"coordinates": "[[[[104.39209000000005,-4.850154],[108.17138687500005,-3.721745195827911],[112.12646500000005,-1.274309],[103.02978499999995,-3.579213]]]]"
}
And because of the quotes around the coordinates themselves, they are not recognised as JSON object by JavaScript.
I've tried to explode the coordinate string and then put it back together manually but it still needs a lot of hacks to get it working.
Any help in getting this to output as a an actual JSON object in PHP would be much appreciated.
I'm trying to get to this:
{
"coordinates": [
[
[
[
104.39209000000005,
-4.850154
],
[
108.17138687500005,
-3.721745195827911
],
[
112.12646500000005,
-1.274309
],
[
103.02978499999995,
-3.579213
]
]
]
]
}
It's pretty jerry-rigged, but you could do this:
$string = json_encode( $database_value ); // same as you're using now
$string = str_replace( '"', '', $string ); // get rid of the quotes
// wrap them back around 'coordinates'
$string = str_replace( 'coordinates', '"coordinates"', $string );
Simply use json_decode before json_encode, like this:
// This comes from the database, of course
$coords = '[[[[104.39209000000005,-4.850154],[108.17138687500005,-3.721745195827911],[112.12646500000005,-1.274309],[103.02978499999995,-3.579213]]]]';
echo json_encode(array(
'coordinates' => json_decode($coords)
));
This will output nested arrays exactly as you desire:
{"coordinates":[[[[104.39209,-4.850154],[108.171386875,-3.7217451958279],[112.126465,-1.274309],[103.029785,-3.579213]]]]}
I'm trying to match two types of strings using the preg_match function in PHP which could be the following.
'_mything_to_newthing'
'_onething'
'_mything_to_newthing_and_some_stuff'
In the third one above, I only want the "mything" and "newthing" so everything that comes after the third part is just some optional text the user could add. Ideally out of the regex would come in the cases of above;
'mything', 'newthing'
'onething'
'mything', 'newthing'
The patterns should match a-zA-Z0-9 if possible :-)
My regex is terrible, so any help would be appreciated!
Thanks in advanced.
Assuming you're talking about _ deliminated text:
$regex = '/^_([a-zA-Z0-9]+)(|_to_([a-zA-Z0-9]+).*)$/';
$string = '_mything_to_newthing_and_some_stuff';
preg_match($regex, $string, $match);
$match = array(
0 => '_mything_to_newthing_and_some_stuff',
1 => 'mything',
2 => '_to_newthing_and_some_stuff',
3 => 'newthing',
);
As far as anything farther, please provide more details and better sample text/output
Edit: You could always just use explode:
$parts = explode('_', $string);
$parts = array(
0 => '',
1 => 'mything',
2 => 'to',
3 => 'newthing',
4 => 'and',
5 => 'some',
6 => 'stuff',
);
As long as the format is consistent, it should work well...