Get KeyValuePair into Array from string using regex - php

I am working on a project and I can't figure out how to go about doing it. I'm trying to do with Regex but fairly new to it.
I have a string such as WHERE MyColumn='1's'' AND MyColumn='Test's''"
I have the following regex in PHP
$found = array();
preg_match("/\s=\s'.*'|\s.*='.*'\s/", $whereQuery, $found);
In my array I have the following
Array
(
[0] => MyColumn='1's''
)
So it's almost there, except I am expecting the following:
Array
(
[0] => MyColumn='1's''
[1] => MyColumn='Test's'
)

You should use preg_match_all with this simplified regex:
preg_match_all('/\w+\h*=\h*\S+/', $whereQuery, $found);
RegEx Demo

Related

How to get a particular string using preg_replace?

i want to get a particular value from string in php. Following is the string
$string = 'users://data01=[1,2]/data02=[2,3]/*';
preg_replace('/(.*)\[(.*)\](.*)\[(.*)\](.*)/', '$2', $str);
i want to get value of data01. i mean [1,2].
How can i achieve this using preg_replace?
How can solve this ?
preg_replace() is the wrong tool, I have used preg_match_all() in case you need that other item later and trimmed down your regex to capture the part of the string you are looking for.
$string = 'users://data01=[1,2]/data02=[2,3]/*';
preg_match_all('/\[([0-9,]+)\]/',$string,$match);
print_r($match);
/*
print_r($match) output:
Array
(
[0] => Array
(
[0] => [1,2]
[1] => [2,3]
)
[1] => Array
(
[0] => 1,2
[1] => 2,3
)
)
*/
echo "Your match: " . $match[1][0];
?>
This enables you to have the captured characters or the matched pattern , so you can have [1,2] or just 1,2
preg_replace is used to replace by regular expression!
I think you want to use preg_match_all() to get each data attribute from the string.
The regex you want is:
$string = 'users://data01=[1,2]/data02=[2,3]/*';
preg_match_all('#data[0-9]{2}=(\[[0-9,]+\])#',$string,$matches);
print_r($matches);
Array
(
[0] => Array
(
[0] => data01=[1,2]
[1] => data02=[2,3]
)
[1] => Array
(
[0] => [1,2]
[1] => [2,3]
)
)
I have tested this as working.
preg_replace is for replacing stuff. preg_match is for extracting stuff.
So you want:
preg_match('/(.*?)\[(.*?)\](.*?)\[(.*?)\](.*)/', $str, $match);
var_dump($match);
See what you get, and work from there.

PHP - What regex code do I need to match this boundary sequence?

I have the following text string:
-asc100-17-asc100-17A-asc100-17BPH-asc100-17ASL
What regex code do I need to extract the values so that they appear in the matches array like this:
-asc100-17
-asc100-17A
-asc100-17BPH
-asc100-17ASL
Thanks in advance!
You may try this:
$str = "-asc100-17-asc100-17A-asc100-17BPH-asc100-17ASL";
preg_match_all('/-asc\d+-[0-9a-zA-Z]+/', $str, $matches);
// Print Result
print_r($matches);
Output:
Array
(
[0] => Array
(
[0] => -asc100-17
[1] => -asc100-17A
[2] => -asc100-17BPH
[3] => -asc100-17ASL
)
)
Based on the very limited information in your question, this works:
-asc100-17[A-Z]*
Debuggex Demo
If you want to capture the post -asc100- code, then use
-asc100-(17[A-Z]*)
Which places 17[the letters] into capture group one.
Might use preg_split with a lookahead as well for your scenario:
print_r(preg_split('/(?=-asc)/', $str, -1, PREG_SPLIT_NO_EMPTY));
Are you trying to break the string in an array? Then why regex is required? This function can handle what you want:
$arr = explode('-asc', '-asc100-17-asc100-17A-asc100-17BPH-asc100-17ASL');
foreach ($arr as $value) {
if(!empty($value)){
$final[] = '-asc'.$value;
}
}
print_r($final);
Output array : Array ( [0] => -asc100-17 [1] => -asc100-17A [2] => -asc100-17BPH [3] => -asc100-17ASL )

Regex - everything except slash

I wrote small routing system, but I have problem with it. My regex is reading slash as a normal string and I've got confused how to make it working. For example:
I defined route home/[a-zA-Z0-9_] that will show profile, but i defined also home/user/\d. When I write down second case, home/user/45, it will write down first case. It will take user/45 as one string. How I can exclude that / using regex.
Have you tried something like this?
[^\/]+
Or better
[a-zA-Z0-9_]+[^\/]+
If you go to regexr.com and put your string ( home/user/45 ) it will select only home,user,45 (excepting the slash /)
You should match the following pattern:
/home\/user\/(\\d+)/
And replace it with the following:
home/user$1
In the first regex, I used a delimiter: a slash. If a delimiter isn't required, remove the first and the last slash.
try home/(?!user/)[a-zA-Z0-9_]
for first case
Try one of these:
<?php
$path = '/home/user/45';
preg_match_all('/home\/(\w+)/', $path, $matches);
/* Would set $matches to Array:
(
[0] => Array
(
[0] => home/user
)
[1] => Array
(
[0] => user
)
)
*/
preg_match_all('/home\/(\w+)/(\d+)/', $path, $matches);
/* Would set $matches to Array:
(
[0] => Array
(
[0] => home/user/45
)
[1] => Array
(
[0] => user
)
[2] => Array
(
[0] => 45
)
)
?>
Since you are trusting the regex on this you really should limit the pattern. And also another trick to make patterns more readable is to use another pattern character:
|^/home/(\w+)/?$|
|^/home/user/(\d+)/?$|

PHP preg_match_all() return dynamic string in the regex

I am using PHP and regular expressions to find:
preg_match_all('/\{\{include:[a-zA-Z0-9_]{25}\}\}/i', $content, $include_matches, PREG_PATTERN_ORDER);
According to the PHP documentation, $include_matches should give me the original string, and the results inside of the parenthesis (PREG_PATTERN_ORDER flag), exactly what I want. The problem is that the result is:
Array
(
[0] => Array
(
[0] => {{include:rec_ih6tk504c23dfdf4a3qqK}}
)
)
I want the result to be:
Array
(
[0] => Array
(
[0] => rec_ih6tk504c23dfdf4a3qqK
)
)
Any idea what I am doing wrong? Thanks.
just use:
preg_match_all('/\{\{include:([a-zA-Z0-9_]{25})\}\}/', $content, $include_matches, PREG_PATTERN_ORDER);
$include_matches= $include_matches[1];
you have to insert a capture-group

trying to filter string with <br> tags using explode, does not work

I get a string that looks like this
<br>
ACCEPT:YES
<br>
SMMD:tv240245ce
<br>
is contained in a variable $_session['result']
I am trying to parse through this string and get the following either in an array or as separate variables
ACCEPT:YES
tv240245ce
I first tried
to explode the string using as the delimiter, and that did not work
then I already tried
$yes = explode(":", strip_tags($_SESSION['result']));
echo print_r($yes);
which gives me an array like so
Array ( [0] => ACCEPT [1] => YESSEED [2] => tv240245ce ) 1
which gives me one of my answers.
Please what would be a great way of trying to achieve what I am trying to achieve?
is there a way to get rid of the first and last?
then use the remaining one as a delimiter to explode the string ?
or what's the best way to go about this ?
This will do it:
$data=preg_split('/\s?<br>\s?/', str_replace('SMMD:','',$data), NULL, PREG_SPLIT_NO_EMPTY);
See example here:
CodePad
You can also skip caring about the spurious <br> and treat the whole string as key:value format with a simple regex like:
preg_match_all('/^(\w+):(.*)/', $text, $result, PREG_SET_ORDER);
This requires that you really have line breaks in it though. Gives you a $result list which is easy to convert into an associative array afterwards:
[0] => Array
(
[0] => ACCEPT:YES
[1] => ACCEPT
[2] => YES
)
[1] => Array
(
[0] => SMMD:tv240245ce
[1] => SMMD
[2] => tv240245ce
)
First, do a str_replace to remove all instances of "SMMD:". Then, Explode on "< b r >\n". Sorry for weird spaced, it was encoding the line break.
Include the new line character and you should get the array you want:
$mystr = str_replace( 'SMMD:', '', $mystr );
$res_array = explode( "<br>\n", $mystr );

Categories