So I'm using the next thing:
<?php
$string='JUD. NEAMT, SAT ROMEDC ALEXANDRE COM. COMENKA, STR. EXAMMS RANTEM, NR.6';
$result=preg_split("/(?:JUD.\s*|\s*SAT\s*|\s*COM\.\s*|\s*STR.\s*|\s*SECTOR\s*|\s*B-DUL\s*|\s*NR\.\s*|\s*ET.\s*|\s*MUN\.\s*|\s*BL.\s*|\s*SC\.\s*|\s*AP\.\s*)/", $string);
array_walk($result,function($value,$key) use (&$result){
if(stristr($value, ","))
{
$result[$key]=explode(",", $value)[0];
}
});
print_r(array_filter($result));
the output would be:
Array
(
[1] => NEAMT
[2] => ROMEDC ALEXANDRE
[3] => COMENKA
[4] => EXAMMS RANTEM
[5] => 6
)
The main problem is that $string is different each time and can contain different parameters like 'SAT' could simply not appear in another string because is replaced by 'SECTOR'.
All these are localization words like House number('NR.') or Town name('JUD').
What I want is to convert the above array into something like this:
Array
(
['JUD'] => NEAMT
['SAT'] => ROMEDC ALEXANDRE
['COM'] => COMENKA
['STR'] => EXAMMS RANTEM
['NR'] => 6
)
I hope you got the idea:
I'm getting from a string 'address' different parameters like apartment number , building number and so on (it depends each time on the customer-- he might be living at a house so there is no apartment number) so having words instead of numbers in the array would help me output the info in different columns.
Any idea is welcome.
Thank you.
$string='JUD. NEAMT, SAT ROMEDC ALEXANDRE COM. COMENKA, STR. EXAMMS RANTEM, NR.6';
//fix missing commas
$string = preg_replace('#([A-Z]+) ([A-Z]+\.)#',"$1, $2",$string);
//a trick to fix non space on `NR.6`
$string = str_replace(['.',' '],['. ',' '],$string);
//get the part seperated by comma, trim to remove spaces
$ex = array_map('trim',explode(',',$string));
//iterate over it
foreach($ex as $e){
//explode the part by space
$new = array_map('trim',explode(' ',$e));
//take the first part as key, remove spaces and dot
$key = trim(array_shift($new),' . ');
//collect via key and implode rest with a space
$coll[$key]=implode(' ',$new);
}
//done
print_r($coll);
Result:
Array
(
[JUD] => NEAMT
[SAT] => ROMEDC ALEXANDRE
[COM] => COMENKA
[STR] => EXAMMS RANTEM
[NR] => 6
)
a fast lane to rome...
Related
I'm converting text from a txt file into an array.I need to shred the texts in this array using regex.
This is the array in my text file.
Array
(
[0] => 65S34523APPLE IS VERY BEAUTIFUL6.000TX786.34563.675 234.89
[1] => 06W01232BOOK IS SUCCESSFUL1.000YJ160.00021.853 496.00
[2] => 67E45643DO YOU HAVE A PEN? 7/56.450EQ9000.3451.432 765.12
)
if I need to explain a line as an example,
input => 65S34523APPLE IS VERY BEAUTIFUL6.000TX786.34563.675 234.89
required sections => 65S34523 APPLE IS VERY BEAUTIFUL 6.000 TX 786.345 63.67 5 234.89
target I want :
Array
(
[0] => 65S34523
[1] => APPLE IS VERY BEAUTIFUL
[2] => TX
[3] => 786.345
)
I need multiple regex patterns to achieve this.I need to shred the data I want in order in a loop.but since there is no specific layout, I don't know what to choose according to the regex patterns.
I've tried various codes to smash this array.
$smash =
array('65S34523APPLE IS VERY BEAUTIFUL6.000TX786.34563.675 234.89',
'06W01232BOOK IS SUCCESSFUL1.000YJ160.00021.853 496.00',
'67E45643DO YOU HAVE A PEN? 7/56.450EQ9000.3451.432 765.12');
I'm trying to foreach and parse the array.For example, I tried to get the text first.
foreach ($smash as $row) {
$delete_numbers = preg_replace('/\d/', '', $smash);
}
echo "<pre>";
print_r($delete_numbers);
echo "</pre>";
While it turned out it was that way.
Array
(
[0] => SAPPLE IS VERY BEAUTIFUL.TX.. .
[1] => WBOOK IS SUCCESSFUL.YJ.. .
[2] => EDO YOU HAVE A PEN? /.EQ.. .
)
Naturally, this is not what I want.Each array has a different structure.So i have to check with if-else too.
As you can see in the example, there is no pure text.Here
TX,YJ,EQ should be deleted.The dots should be wiped using apples.The first letters at the beginning of the text should
be removed.The remaining special characters must be removed.
I have tried many of the above.I have looked at alternative examples.
AS A RESULT;
I'm in a dead end.
Code: (Demo)
$smash = ['65S34523APPLE IS VERY BEAUTIFUL6.000TX786.34563.675 234.89',
'06W01232BOOK IS SUCCESSFUL1.000YJ160.00021.853 496.00',
'67E45643DO YOU HAVE A PEN? 7/56.450EQ9000.3451.432 765.12'];
foreach ($smash as $line) {
$result[] = preg_match('~(\w+\d)(\D+)[^A-Z]+([A-Z]{2})(\d+\.\d{3})~', $line, $out) ? array_slice($out, 1) : [];
}
var_export($result);
Output:
array (
0 =>
array (
0 => '65S34523',
1 => 'APPLE IS VERY BEAUTIFUL',
2 => 'TX',
3 => '786.345',
),
1 =>
array (
0 => '06W01232',
1 => 'BOOK IS SUCCESSFUL',
2 => 'YJ',
3 => '160.000',
),
2 =>
array (
0 => '67E45643',
1 => 'DO YOU HAVE A PEN? ',
2 => 'EQ',
3 => '9000.345',
),
)
My pattern assumes:
The first group will consist of numbers and letters and conclude with a digit.
The second group contains no digits.
The third group is consistently 2 uppercase letters.
The fourth group will reliably have three decimal places.
p.s. If you don't want that pesky trailing space after PEN?, you could use this:
https://3v4l.org/9XpA6
~(\w+\d)([^\d ]+(?: [^\d ]+)*) ?[^A-Z]+([A-Z]{2})(\d+\.\d{3})~
I have an array variable which contain values like this:
$items = array(
"tbFrench",
"eaItaly1",
"discount21",
"kkMM5",
"NbndA",
"fcMNSS334"
);
i nedd to remove last character of string from this array values if the last character contain number, for example:
$newItems = array();
foreach($items as $item){
$newItems[] = $this->removeLastCharacter($item);
}
print_r($newItems);
....
function removeLastCharacter($string){
// ????
}
i want the result to look like this when i print_r the $newItems variable:
Array ( [0] => tbFrench [1] => eaItaly [2] => discount2 [3] => kkMM [4] => NbndA [5] => fcMNSS33 )
You could use regular expressions to remove the last digit.
function removeLastCharacter($string){
return preg_replace('[\d$]', '', $string);
}
\d matches every digit and $ references the end of the string. So this will only replace the last character if it is a digit at the end.
You can do a RegEx replacement over all the items in an array by simply providing the array as the subject, like so:
$items = preg_replace('/^d$/', '', $items);
There's no need to put it into a function at all - print_r($items) outputs:
Array
(
[0] => tbFrench
[1] => eaItaly
[2] => discount2
[3] => kkMM
[4] => NbndA
[5] => fcMNSS33
)
If you want to replace all trailing digits you can use /^\d+$/
Give a try with below code if it solve your problem
$items = array(
"tbFrench",
"eaItaly1",
"discount21",
"kkMM5",
"NbndA",
"fcMNSS334"
);
$newArr=array();
foreach($items as $item){
$data = preg_replace('[\d$]','',$item);
array_push($newArr,$data);
}
print_r($newArr);
Why not simply?
print_r( preg_replace( '/\d+$/', "", $items ) ); // preg_replace accepts an array as argument, pass yours directly, no need for a loop.
Array
(
[0] => tbFrench
[1] => eaItaly
[2] => discount
[3] => kkMM
[4] => NbndA
[5] => fcMNSS
)
Regex Explanation:
\d+ — matches a digit (equal to [0-9])
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
$ — asserts position at the end of a line
There is a quick trick using rtrim.
$result = rtrim($str,"0..9");
second argument is a range using 2 dots ".."
You are done !!!
Given the following string (Yes, STRING, not Array), I want to add double quotes around the names of countries.
$string = "Array ( [0] => Array ( [nicename] => Afghanistan [phonecode] => 93 ) [1] => Array ( [nicename] => United States [phonecode] => 1 )";
I want the following string:
Array ( [0] => Array ( [nicename] => "Afghanistan" [phonecode] => 93 ) [1] => Array ( [nicename] => "United States" [phonecode] => 1 )
How can I do that?
Note: This String shows only two countries, but the actual data will have more than a hundred counties.
I was thinking of doing something like
$string = preg_replace("/[[:alpha:]]/", "/\"[[:alpha:]]\"/", $string);
But the problem is that for the second argument, (1) how would PHP know what that character class [[:alpha:]] is and (2) The names of countries might contain spaces in addition to alphabetical characters.
You should really be doing this where the array is built but it can be done with a regex...
You need to capture everything after nicename until a [ or ) (e.g. if the nicename is at the end or middle of the "array").
Using something like:
(\[nicename\] => )([^\[)]+)
should accomplish that, then you need to quote the found country name:
$1"$2"
Demo: https://regex101.com/r/7TeUQu/1
this has extra spaces after the country name since whitespace were allowed there. In PHP we'll need to use preg_replace_callback and the trim function to resolve this.
$regex = '/(\[nicename\] => )([^\[)]+)/';
$replace = '$1"$2" ';
$string = 'Array ( [0] => Array ( [nicename] => Afghanistan [phonecode] => 93 ) [1] => Array ( [nicename] => United States [phonecode] => 1 )';
$string = preg_replace_callback($regex, function($match) {
return $match[1] . '"' . trim($match[2]) . '" ';
}, $string);
echo $string;
PHP Demo: https://eval.in/699678
Here you go:
$string = preg_replace('/(\[nicename\] =>) ([a-zA-Z ]+) \[/', '$1 "$2" [', $string);
I have a string variable in php, with look like "0+1.65+0.002-23.9", and I want to split in their individual values.
Ex:
0
1.65
0.002
-23.9
I Try to do with:
$keys = preg_split("/^[+-]?\\d+(\\.\\d+)?$/", $data);
but not work I expected.
Can anyone help me out? Thanks a lot in advance.
Like this:
$yourstring = "0+1.65+0.002-23.9";
$regex = '~\+|(?=-)~';
$splits = preg_split($regex, $yourstring);
print_r($splits);
Output (see live php demo):
[0] => 0
[1] => 1.65
[2] => 0.002
[3] => -23.9
Explanation
Our regex is +|(?=-). We will split on whatever it matches
It matches +, OR |...
the lookahead (?=-) matches a position where the next character is a -, allowing us to keep the -
Then we split!
Option 2 if you decide you also want to keep the + Character
(?=[+-])
This regex is one lookahead that asserts that the next position is either a plus or a minus. For my sense of esthetics it's quite a nice solution to look at. :)
Output (see online demo):
[0] => 0
[1] => +1.65
[2] => +0.002
[3] => -23.9
Reference
Lookahead and Lookbehind Zero-Length Assertions
Mastering Lookahead and Lookbehind
You could try this
$data = ' 0 1.65 0.002 -23.9';
$t = str_replace( array(' ', ' -'), array(',',',-'), trim($data) );
$ta = explode(',', $t);
print_r($ta);
Which gives you an array containing each field like so:
Array
(
[0] => 0
[1] => 1.65
[2] => 0.002
[3] => -23.9
)
RE: Your comment: The originals values are in a string variable only separated for a sign possitive or negative
$data = ' 0+1.65+0.002-23.9 ';
$t = str_replace( array('-', '+'), array(',-',',+'), trim($data) );
$ta = explode(',', $t);
print_r($ta);
which gives a similiar answer but with the correct inputs and outputs
Array
(
[0] => 0
[1] => +1.65
[2] => +0.002
[3] => -23.9
)
I'm processing a single string which contains many pairs of data. Each pair is separated by a ; sign. Each pair contains a number and a string, separated by an = sign.
I thought it would be easy to process, but i've found that the string half of the pair can contain the = and ; sign, making simple splitting unreliable.
Here is an example of a problematic string:
123=one; two;45=three=four;6=five;
For this to be processed correctly I need to split it up into an array that looks like this:
'123', 'one; two'
'45', 'three=four'
'6', 'five'
I'm at a bit of dead end so any help is appreciated.
UPDATE:
Thanks to everyone for the help, this is where I am so far:
$input = '123=east; 456=west';
// split matches into array
preg_match_all('~(\d+)=(.*?);(?=\s*(?:\d|$))~', $input, $matches);
$newArray = array();
// extract the relevant data
for ($i = 0; $i < count($matches[2]); $i++) {
$type = $matches[2][$i];
$price = $matches[1][$i];
// add each key-value pair to the new array
$newArray[$i] = array(
'type' => "$type",
'price' => "$price"
);
}
Which outputs
Array
(
[0] => Array
(
[type] => east
[price] => 123
)
)
The second item is missing as it doesn't have a semicolon on the end, i'm not sure how to fix that.
I've now realised that the numeric part of the pair sometimes contains a decimal point, and that the last string pair does not have a semicolon after it. Any hints would be appreciated as i'm not having much luck.
Here is the updated string taking into account the things I missed in my initial question (sorry):
12.30=one; two;45=three=four;600.00=five
You need a look-ahead assertion for this; the look-ahead matches if a ; is followed by a digit or the end of your string:
$s = '12.30=one; two;45=three=four;600.00=five';
preg_match_all('/(\d+(?:.\d+)?)=(.+?)(?=(;\d|$))/', $s, $matches);
print_r(array_combine($matches[1], $matches[2]));
Output:
Array
(
[12.30] => one; two
[45] => three=four
[600.00] => five
)
I think this is the regex you want:
\s*(\d+)\s*=(.*?);(?=\s*(?:\d|$))
The trick is to consider only the semicolon that's followed by a digit as the end of a match. That's what the lookahead at the end is for.
You can see a detailed visualization on www.debuggex.com.
You can use following preg_match_all code to capture that:
$str = '123=one; two;45=three=four;6=five;';
if (preg_match_all('~(\d+)=(.+?);(?=\d|$)~', $str, $arr))
print_r($arr);
Live Demo: http://ideone.com/MG3BaO
$str = '123=one; two;45=three=four;6=five;';
preg_match_all('/(\d+)=([a-zA-z ;=]+)/', $str,$matches);
echo '<pre>';
print_r($matches);
echo '</pre>';
o/p:
Array
(
[0] => Array
(
[0] => 123=one; two;
[1] => 45=three=four;
[2] => 6=five;
)
[1] => Array
(
[0] => 123
[1] => 45
[2] => 6
)
[2] => Array
(
[0] => one; two;
[1] => three=four;
[2] => five;
)
)
then y can combine
echo '<pre>';
print_r(array_combine($matches[1],$matches[2]));
echo '</pre>';
o/p:
Array
(
[123] => one; two;
[45] => three=four;
[6] => five;
)
Try this but this code is written in c#, you can change it into php
string[] res = Regex.Split("123=one; two;45=three=four;6=five;", #";(?=\d)");
--SJ