I'm working with a string containing parameters, separated by some special characters in PHP with preg_match
An example could be like this one, which has four parameters.
1stparm?#?1111?#?2ndParm?#?2222?#?3rdParm?#?3333?#?4thparm?#?444?#?
Each parameter name is followed by ?#?, and its value is right next to it, ending with ?#? (note: values can be strings or numbers, and even special characters)
I've probably overcomplicated my regex, which works in SOME cases, but not if I search for the last parameter in the string..
This example returns 2222 as the correct value (in group 1) for 2ndParm
(?:.*)2ndParm\?#\?(.*?)\?#\?(?=.)(.*)
but it fails if 2ndParm is the last one in the string as in the following example:
1stparm?#?1111?#?2ndParm?#?2222?#?
I'd also appreciate help in just returning one group with my result.. i havent been able to do so, but since I always get the one I'm interested in group 1, I can get it easily anyway.
Without regex:
$str ='1stparm?#?1111?#?2ndParm?#?2222?#?3rdParm?#?3333?#?4thparm?#?444?#?';
$keyval = explode('?#?', trim($str, '?#'));
$result = [];
foreach($keyval as $item) {
[$key, $result[$key]] = explode('?#?', $item);
}
print_r($result);
demo
You don't need to use a regex for everything, and you should have a serious talk with whoever invented this horrid format about the fact that JSON, YAML, TOML, XML, etc exist.
function bizarre_unserialize($in) {
$tmp = explode('?#?', $in);
$tmp = array_filter($tmp); // remove empty
$tmp = array_map(
function($a) { return explode('?#?', $a); },
$tmp
);
// rearrange to key-value
return array_combine(array_column($tmp, 0), array_column($tmp, 1));
}
$input = '1stparm?#?1111?#?2ndParm?#?2222?#?3rdParm?#?3333?#?4thparm?#?444?#?';
var_dump(
bizarre_unserialize($input)
);
Output:
array(4) {
["1stparm"]=>
string(4) "1111"
["2ndParm"]=>
string(4) "2222"
["3rdParm"]=>
string(4) "3333"
["4thparm"]=>
string(3) "444"
}
You can use
(?P<key>.+?)
\Q?#?\E
(?P<value>.+?)
\Q?#?\E
in verbose mode, see a demo on regex101.com.
The \Q...\E construct disables the ? and # "super-powers" (no need to escape them here).
In PHP this could be
<?php
$string = "1stparm?#?1111?#?2ndParm?#?2222?#?3rdParm?#?3333?#?4thparm?#?444?#?";
$regex = "~(?P<key>.+?)\Q?#?\E(?P<value>.+?)\Q?#?\E~";
preg_match_all($regex, $string, $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
echo $match["key"] . " = " . $match["value"] . "\n";
}
?>
Which yields
1stparm = 1111
2ndParm = 2222
3rdParm = 3333
4thparm = 444
Or shorter:
$result = array_map(
function($x) {return array($x["key"] => $x["value"]);}, $matches);
print_r($result);
Related
In the database I'm working on there's a string like this
1-Test Response|9-DNC|
This can have up to 9 pipe delimited items.
What I'm looking for advice on is the best possible way to take this string and turn it into an array with the number as the key and the string as the value.
I really suck with Regex. Can someone point me in the right direction?
Since it's not your fault you have the DB structured this way, please accept my sincere condolences. It must be hell working with this. Meh.
Now, to the point. You do not need regex to work with this string. If you have a problem and you want to solve it with regex, you have two problems.
Instead, use explode().
$testString = "1-Test Response|9-DNC|";
$result = [];
$explodedByPipeString = explode("|", $testString);
foreach($explodedByPipeString as $k => $v)
{
$explodedByDashString = explode("-", $v);
if(is_numeric($explodedByDashString[0]))
{
$result[$explodedByDashString[0]] = $explodedByDashString[1];
}
}
var_dump($result);
This gives
array(2) {
[1]=>
string(13) "Test Response"
[9]=>
string(3) "DNC"
}
Here's how I went about it for anyone else wondering
$SurveyOptions = preg_match_all('/(\d+)-([^|]+)/',
$res['survey_response_digit_map'], $matches);
$finalArray = array_combine($matches[1], $matches[2]);
Pretty straight forward.
Assuming the delimters: - and | do not exist in the keys or values, here is another non-regex way to tackle the string:
Code: (Demo)
$string = '1-Test Response|9-DNC|';
$string = str_replace(['-', '|'], ['=', '&'], $string); // generates: 1=Test Response&9=DNC&
parse_str($string, $result);
var_export($result);
Output:
array (
1 => 'Test Response',
9 => 'DNC',
)
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 want to grab a text with PHP just like for an example, There is a data "The apple=10" and I want to grab only the numbers from the data which looks exactly like that. I mean, the number's place would be after 'equals'.
and my problem is that the number from the source can be 2 or 3 characters or on the other word it is inconstant.
please help me to solve them :)
$string = "Apple=10 | Orange=3 | Banana=7";
$elements = explode("|", $string);
$values = array();
foreach($elements as $element)
{
$element = trim($element);
$val_array = explode("=", $element);
$values[$val_array[0]] = $val_array[1];
}
var_dump($values);
Output:
array(3) {
["Apple"]=> string(2) "10"
["Orange"]=> string(1) "3"
["Banana"]=> string(1) "7"
}
Hope thats how you need it :)
Well, php is a bit lazy about int conversion, so 12345blablabla can be converted to 12345:
$value = intval(substr($str, strpos($str, '=') + 1));
Of course, this is not the cleanest way but it is simple. If you want something cleaner, you could use a regexp:
preg_match ('#=([0-9]+)#', $str, $matches);
$value = intval($matches[1]) ;
Try the below code:
$givenString= "The apple=10";
$required_string = substr($givenString, strpos($givenString, "=") + 1);
echo "output = ".$required_string ; // output = 10
Using strpos() function, you can Find the position of the first occurrence of a substring in a string
and substr() function, Return part of a string.
If I have a string "123x456x78", how could I explode it to return an array containing "123" as the first element and "456" as the second element? Basically, I want to take strings that are followed by "x" (which is why "78" should be thrown out). I've been messing around with regular expressions, but am having trouble.
Thanks!
EDIT: if the string were "123x456x78x" I would need three elements: "123", "456", "78". Basically, for each region following an "x", I need to record the string up until the next "x".
Loads of different ways, but here's a RegEx as you were trying that:
$str = "123x456x78";
preg_match_all("/(\d+)x/", $str, $matches);
var_dump($matches[1]);
Output:
array(2) { [0]=> string(3) "123" [1]=> string(3) "456" }
$arr = explode("x", "123x456x78");
and then
unset($arr[2]);
if you really can't stand that poor 78.
use explode
$string='123x456x78';
$res = explode('x', $string);
if(count($res) > 0) {
echo $res[0];
if(count($res) > 1) {
echo $res[1];
}
}
$var = "123x456x78";
$array = explode("x", $var);
array_pop($array);
To explode AND remove the last result:
$string='123x456x78'; // original string
$res = explode('x', $string); // resulting array, exploded by 'x'
$c = count($res) - 1; // last key #, since array starts at 0 subtract 1
unset($res[$c]); // unset that last value, leaving you with everything else but that.
While I'm all for regular expressions, in this case it might be easier to just use PHP's array functions...
$result=array_slice(explode('x',$yourstring),0,-1);
This should work because only the last element returned by explode won't be followed by an 'x'. Not sure if explode will add an empty string as the last element if it ends on 'x' though, you might have to test that...
Use this below code to explode. It works well!
<?php
$str='123x456x78';
$res=explode('x',$str);
unset($res[count($res)-1]); // remove last array element
print_r($res);
?>
I have several strings of the format
AA11
AAAAAA1111111
AA1111111
Which is the best (most efficient) way to separate the alphabetic and numeric components of the string?
If they're all a series of alpha, followed by a series of numeric, with no non-alphameric characters, then sscanf() is probably more efficient than regexp
$example = 'AAA11111';
list($alpha,$numeric) = sscanf($example, "%[A-Z]%d");
var_dump($alpha);
var_dump($numeric);
preg_split should do the job fine.
preg_split('/(\w+)/', $input, -1, PREG_SPLIT_DELIM_CAPTURE);
The preg library is surprisingly efficient in handling strings, so I would assume it to be more efficient than anything you can write by hand, using more primitive string functions. But do a test and see for your self.
Here is a working example using preg_split():
$strs = array( 'AA11', 'AAAAAA1111111', 'AA1111111');
foreach( $strs as $str)
foreach( preg_split( '/([A-Za-z]+)/', $str, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY) as $temp)
var_dump( $temp);
This outputs:
string(2) "AA"
string(2) "11"
string(6) "AAAAAA"
string(7) "1111111"
string(2) "AA"
string(7) "1111111"
Instead of using RegEx straight away you can add one extra check for example:
if (ctype_alpha($testcase)) {
// Return the value it's only letters
} else if(ctype_digit($testcase)) {
// Return the value it's only numbers
} else {
//RegEx your string to split nums and alphas
}
EDIT: Obviously my answer didn't give an evidence which will perform better, that's why I did a test that produced the following result:
preg_split took 5.3319189548492 seconds
sscanf took 3.4432129859924 seconds
And the answer should have been sscanf
Here's the code that produced the result:
$string = "AAAAAAAAAA111111111111111";
$count = 1000000;
function prSplit($string) {
return preg_split( '/([A-Za-z]+)/', $string, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
}
function sScanfTest($string) {
return sscanf($string, "%[A-Z]%[0-9]");
}
function microtime_float()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
$startTime1 = microtime_float();
for($i=0; $i<$count; ++$i) {
prSplit($string);
}
$time1 = microtime_float() - $startTime1;
echo '1. preg_split took '.$time1.' seconds<br />';
$startTime2 = microtime_float();
for($i=0; $i<$count; ++$i) {
sScanfTest($string);
}
$time2 = microtime_float() - $startTime2;
echo '2. sscanf took '.$time2.' seconds';
This seems to work but when you try to pass something like "111111", it doesn't.
In my application, I am expecting several scenarios and what seems to be doing the trick is this
$referenceNumber = "AAA12132";
$splited = preg_split('/(\d+)/', $referenceNumber, -1, PREG_SPLIT_DELIM_CAPTURE);
var_dump($splited);
Note:
Getting an array of 2 elements, it means the 0th index is the alpha while the 1st is the numerics.
Getting array of just 1 element, means the 0th element is the numeric and no alphas.
If you get more than 2 array items, then your string must be in this format “AAA1323SDC”
So given the above, you can play around with it based on your use case.
Cheers!