I have pretty long string to parse, that looks like that (part of it)
$string = 'FIRM_ID = MC0356400000; TAG = EQTV; CURR_CODE = SUR; CLIENT_CODE = FR334; LIMIT_KIND = 1; OPEN_BALANCE = 4822.84; OPEN_LIMIT = 0.00; LEVERAGE = 0;'
I need to get values for php variables from that string, which I do with preg_match:
preg_match("/FIRM_ID = (.*?);/", $string, $m);
$firm_id = trim($m[1]);
preg_match("/CLIENT_CODE = (.*?);/", $string, $m);
$client_code = trim($m[1]);
... and so on
I was wondering is there a way to do the same in one line? May be with preg_replace or other functions, so I would not have to declare $m variable first and then take out from that [1] element.
So the code supposed to look like
$firm_id = somefunction($string);
$client_code = somefunction($string);
Its not practical question, more theoretical. I know how to get result that I need, I want to know if there is a simpler and more elegant way.
Thanks.
If you remove spaces and replace ; with &, you can do this:
parse_str(str_replace([' ', ';'], ['', '&'], $string), $result);
Which yields an easy to use associative array:
Array
(
[FIRM_ID] => MC0356400000
[TAG] => EQTV
[CURR_CODE] => SUR
[CLIENT_CODE] => FR334
[LIMIT_KIND] => 1
[OPEN_BALANCE] => 4822.84
[OPEN_LIMIT] => 0.00
[LEVERAGE] => 0
)
So just echo $result['FIRM_ID'];
Match and capture key-value pairs and then combine into an array:
$re = '/(\w+)\s*=\s*([^;]*)/';
$str = 'FIRM_ID = MC0356400000; TAG = EQTV; CURR_CODE = SUR; CLIENT_CODE = FR334; LIMIT_KIND = 1; OPEN_BALANCE = 4822.84; OPEN_LIMIT = 0.00; LEVERAGE = 0;';
preg_match_all($re, $str, $matches);
print_r(array_combine($matches[1],$matches[2]));
See the PHP demo, result:
Array
(
[FIRM_ID] => MC0356400000
[TAG] => EQTV
[CURR_CODE] => SUR
[CLIENT_CODE] => FR334
[LIMIT_KIND] => 1
[OPEN_BALANCE] => 4822.84
[OPEN_LIMIT] => 0.00
[LEVERAGE] => 0
)
The regex is
/(\w+)\s*=\s*([^;]*)/
See is demo online.
Details:
(\w+) - Group 1: one or more word chars
\s*=\s* - a = enclosed with optional whitespace(s)
([^;]*) - Group 2: zero or more chars other than ;.
To "initialize" the variables each at a time, you may use a
$var_name = 'FIRM_ID';
$re = '/' . $var_name . '\s*=\s*\K[^;]*/';
$str = 'FIRM_ID = MC0356400000; TAG = EQTV; CURR_CODE = SUR; CLIENT_CODE = FR334; LIMIT_KIND = 1; OPEN_BALANCE = 4822.84; OPEN_LIMIT = 0.00; LEVERAGE = 0;';
preg_match($re, $str, $m);
print_r($m);
See the PHP demo.
The \K is the match reset operator that omits all text matched so far within the current match iteration.
You can also use list function after preg_match_all :
preg_match_all('/(\w[\w-]*)\h*=\h*([^;\h]+);/', $string, $matches);
list($firmId, $tag, $currCode, $clientCode, $limitKind, $openBalance, $openLimit, $leverage) = $matches[2];
echo $firmId;
//=> MC0356400000
echo $tag;
//=> EQTV
echo $clientCode;
//=> FR334
echo $openBalance;
//=> 4822.84
Related
I have an array
Array
(
[0] => "http://example1.com"
[1] => "http://example2.com"
[2] => "http://example3.com"
...
)
And I want to replace the http with https of each elements using RegEx. I tried:
$Regex = "/http/";
$str_rpl = '${1}s';
...
foreach ($url_array as $key => $value) {
$value = preg_replace($Regex, $str_rpl, $value);
}
print_r($url_array);
But the result array is still the same. Any thought?
You actually print an array without changing it. Why do you need regex for this?
Edited with Casimir et Hippolyte's hint:
This is a solution using regex:
$url_array = array
(
0 => "http://example1.com",
1 => "http://example2.com",
2 => "http://example3.com",
);
$url_array = preg_replace("/^http:/i", "https:", $url_array);
print_r($url_array);
PHP Demo
Without regex:
$url_array = array
(
0 => "http://example1.com",
1 => "http://example2.com",
2 => "http://example3.com",
);
$url_array = str_replace("http://", "https://", $url_array);
print_r($url_array);
PHP Demo
First of all, you are not modifying the array values at all. In your example, you are operating on the copies of array values. To actually modify array elements:
use reference mark
foreach($foo as $key => &$value) {
$value = 'new value';
}
or use for instead of foreach loop
for($i = 0; $i < count($foo); $i++) {
$foo[$i] = 'new value';
}
Going back to your question, you can also solve your problem without using regex (whenever you can, it is always better to not use regex [less problems, simpler debugging, testing etc.])
$tmp = array_map(static function(string $value) {
return str_replace('http://', 'https://', $value);
}, $url_array);
print_r($tmp);
EDIT:
As Casimir pointed out, since str_replace can take array as third argument, you can just do:
$tmp = str_replace('http://', 'https://', $url_array);
This expression might also work:
^http\K(?=:)
which we can add more boundaries, and for instance validate the URLs, if necessary, such as:
^http\K(?=:\/\/[a-z0-9_-]+\.[a-z0-9_-]+)
DEMO
Test
$re = '/^http\K(?=:\/\/[a-z0-9_-]+\.[a-z0-9_-]+)/si';
$str = ' http://example1.com ';
$subst = 's';
echo preg_replace($re, $subst, trim($str));
Output
https://example1.com
The expression is explained on the top right panel of regex101.com, if you wish to explore/simplify/modify it, and in this link, you can watch how it would match against some sample inputs, if you like.
RegEx Circuit
jex.im visualizes regular expressions:
I am trying to get the integer on the left and right for an input from the $str variable using REGEX. But I keep getting the commas back along with the integer. I only want integers not the commas. I have also tried replacing the wildcard . with \d but still no resolution.
$str = "1,2,3,4,5,6";
function pagination()
{
global $str;
// Using number 4 as an input from the string
preg_match('/(.{2})(4)(.{2})/', $str, $matches);
echo $matches[0]."\n".$matches[1]."\n".$matches[1]."\n".$matches[1]."\n";
}
pagination();
How about using a CSV parser?
$str = "1,2,3,4,5,6";
$line = str_getcsv($str);
$target = 4;
foreach($line as $key => $value) {
if($value == $target) {
echo $line[($key-1)] . '<--low high-->' . $line[($key+1)];
}
}
Output:
3<--low high-->5
or a regex could be
$str = "1,2,3,4,5,6";
preg_match('/(\d+),4,(\d+)/', $str, $matches);
echo $matches[1]."<--low high->".$matches[2];
Output:
3<--low high->5
The only flaw with these approaches is if the number is the start or end of range. Would that ever be the case?
I believe you're looking for Regex Non Capture Group
Here's what I did:
$regStr = "1,2,3,4,5,6";
$regex = "/(\d)(?:,)(4)(?:,)(\d)/";
preg_match($regex, $regStr, $results);
print_r($results);
Gives me the results:
Array ( [0] => 3,4,5 [1] => 3 [2] => 4 [3] => 5 )
Hope this helps!
Given your function name I am going to assume you need this for pagination.
The following solution might be easier:
$str = "1,2,3,4,5,6,7,8,9,10";
$str_parts = explode(',', $str);
// reset and end return the first and last element of an array respectively
$start = reset($str_parts);
$end = end($str_parts);
This prevents your regex from having to deal with your numbers getting into the double digits.
I have string like following format:
$string ='[(id,name,top,left,width,height,depth)filename|filename][(id,name,top,left,width,height,depth)filename|filename]';
I want to extract string for square and simple bracket and result set would be like
[0] =>(id,name,top,left,width,height,depth)
[1] => filename
[2] => filename
How can i do it? I have tried below code:
explode("[" , rtrim($string, "]"));
But not working properly.
You can use regex for this,
$re = "/\\[\\((.*?)\\)(.*?)\\]/s";
$str = "[(id,name,top,left,width,height,depth)filename|filename][(id,name,top,left,width,height,depth)filename|filename]'";
preg_match_all($re, $str, $matches);
output
matches[1][0] => id,name,top,left,width,height,depth
matches[1][1] => filename|filename
See
regex
Use this code
$string ='[(id,name,top,left,width,height,depth)filename|filename][(id,name,top,left,width,height,depth)filename|filename]';
$array = explode('][',$string);
$array = array_unique(array_filter($array));
$singleArr = array();
$singleArr[] = str_replace('[','',$array['0']);
$singleArr[] = str_replace(']','',$array['1']);
$singleArr = array_unique($singleArr);
//print_r($singleArr);
$blankArr = array();
foreach($singleArr as $val)
{
$first = explode('|',$val);
$blankArr['0'] = substr($first['0'],0,-8);
$blankArr['1'] = substr($first['0'],-8);
$blankArr['2'] = $first['1'];
}
print_r($blankArr);
I use coordinates for shops and manually add the coordinates as lang and long to database. Sometimes by mistake, approving the coordinate.
Let me exlain by an example.
For example;
Lang is 33.4534543543 .But by mistake sometimes i push keyboard and it becomes like,
33.4534543543<
or
33.4534543543,
or
,(space)33.4534543543<
How can I get only the 33.4534543543?
preg_match_all
To find matches from a string containing multiple matches, you would use preg_match_all:
$strings = "33.4534543543<
33.4534543543,
, 33.4534543543<";
$pattern = "!(\d+\.\d+)!";
preg_match_all($pattern,$strings,$matches);
print_r($matches[0]);
Output
Array
(
[0] => 33.4534543543
[1] => 33.4534543543
[2] => 33.4534543543
)
preg_match
To find the match from a single string you could use preg_match.
$string = "33.4534543543<";
$pattern = "!(\d+\.\d+)!";
if(preg_match($pattern,$string,$match)){
print($match[0]);
}
Output
33.4534543543
preg_replace
To replace anything that is not what you want in your existing string you would use preg_replace:
$string = preg_replace('![^\d.]!','',$string);
An example:
$strings = "33.4534543543<
33.4534543543,
, 33.4534543543<";
$strings_exp = explode("\n",$strings);
$output = '';
foreach($strings_exp as $string){
$output.= "String '$string' becomes ";
$new_string = preg_replace('![^0-9.]!','',$string);
$output.= "'$new_string'\n";
}
echo $output;
output
String '33.4534543543<' becomes '33.4534543543'
String '33.4534543543,' becomes '33.4534543543'
String ', 33.4534543543<' becomes '33.4534543543'
Sound like you want to do a preg_match: http://phpfiddle.org/main/code/z6q-a1d
$old_vals = array(
'33.4534543543<',
'33.4534543543,',
', 33.4534543543<'
);
$new_vals = array();
foreach ($old_vals as $val) {
preg_match('(\d*\.?\d+)',$val, $match);
array_push($new_vals, $match[0]);
}
print_r($new_vals);
Output
Array (
[0] => 33.4534543543,
[1] => 33.4534543543,
[2] => 33.4534543543
)
Is there an inbuilt PHP function to replace multiple values inside a string with an array that dictates exactly what is replaced with what?
For example:
$searchreplace_array = Array('blah' => 'bleh', 'blarh' => 'blerh');
$string = 'blah blarh bleh bleh blarh';
And the resulting would be: 'bleh blerh bleh bleh blerh'.
You are looking for str_replace().
$string = 'blah blarh bleh bleh blarh';
$result = str_replace(
array('blah', 'blarh'),
array('bleh', 'blerh'),
$string
);
// Additional tip:
And if you are stuck with an associative array like in your example, you can split it up like that:
$searchReplaceArray = array(
'blah' => 'bleh',
'blarh' => 'blerh'
);
$result = str_replace(
array_keys($searchReplaceArray),
array_values($searchReplaceArray),
$string
);
$string = 'blah blarh bleh bleh blarh';
$trans = array("blah" => "blerh", "bleh" => "blerh");
$result = strtr($string,$trans);
You can check the manual for detailed explanation.
IN CASE some one is looking for replacing same strings with different values ( per occurence ).. Example, to replace all ## by numbers++ OR values from an array-
$split_at = '##';
$string = "AA ## BB ## CC ## DD";
$new_string = '';
// echo $string;
$replace_num = 1;
$replace_arr = ['first' , 'second' , 'third'];
$split_at_cnt = substr_count($string, $split_at);
for ($split=0; $split <= $split_at_cnt; $split++)
{
$new_string .= str_replace('##', ($replace_num++)." : ".$replace_arr[$split], substr( $string, 0, strpos($string, $split_at)+strlen($split_at)));
$string = substr($string, strpos($string, $split_at)+strlen($split_at));
}
echo $new_string;
str_replace() does that.
You can check the manual for more detailed explanation.
For what you've got there, just pass that array into str_replace as both the search and replace (using array_keys on the search parameter if you want to keep the array as-is).