Preg_split or preg_match - php

I have a little problem i need to sort. I want to either remove a part of a string or split it.
So basically i have this: One-1, Two-2, Three-3
What I want to end up doing is splitting into 2 variables where i have "One, Two, Three" and "1, 2, 3" , Im not sure if i can split it into two or if i have to remove the part after "-" first then do it again to remove the bit before "-" to end up with two variables. Anyways I have had a look and seems that preg_split or preg_match may work, but have no idea about preg patterns.
This is what i have so far :
$string = 'One-1, Two-2, Three-3';
$pattern = '????????????';
preg_match_all($pattern, $string, $matches);
print_r($matches);
EDIT: Sorry my Question was worded wrong:
Basically if someone could help me with the preg pattern to either split the Values so I have an array of One, Two Three and 1, 2, 3
Any guidance appreciated
Ian
----------------EDIT--------------
I have another question if I can, how would the preg_match change if I had this
:
"One Object-1, Two Object-2" So that now I have more than one word before the "-" which want to be stored together and the "1" on its own?

Try this :
$string = 'One-1, Two-2, Three-3';
$pattern = '/(?P<first>\w+)-(?P<second>\w+)/';
preg_match_all($pattern, $string, $matches);
print_r($matches['first']);
print_r($matches['second']);
Output:
Array ( [0] => One [1] => Two [2] => Three )
Array ( [0] => 1 [1] => 2 [2] => 3 )

If you always have a "-" use this instead:
$string = "One-1";
$args = explode("-", $string);
// $args[0] would have One
// $args[1] would have 1
You can read more about this function here: http://php.net/manual/en/function.explode.php

This should work:
[^-]+
But why use regexp at all? You can simply explode by "-"

Related

preg_split to seperate input [duplicate]

This question already has answers here:
Split string on spaces except words in quotes
(4 answers)
Closed 3 years ago.
I'm building a website using PHP.
I am using a preg_split() to separate a given string which looks like +word1 -word2 -"word word".
But I need them in the following form +word1, -word2, -"word word".
Currently, I have this one:
$words = preg_split("/[\s\"]*\"([^\"]+)\"[\s\"]*|" . "[\s\"]*'([^']+)'[\s\"]*|" . "[\s\"]+/", $search_expression, 0, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
but it didn't work as I wish: I need to do it in this way to get it work:
+word1 -word2 '-"word word"'.
Does someone have a better regex or idea?
One option is to match from a double quote till a double quote and don't split on that match using SKIP FAIL. Then match 1+ horizontal whitespace chars to split on.
"[^"]+"(*SKIP)(*FAIL)|\h+
Regex demo | Php demo
For example
$search_expression = '+word1 -word2 -"word word"';
$words = preg_split("~\"[^\"]+\"(*SKIP)(*FAIL)|\h+~", $search_expression);
print_r($words);
Output
Array
(
[0] => +word1
[1] => -word2
[2] => -"word word"
)
A simpler expression with greedy ? works for matching your examples:
preg_match_all('/[+-][^+-]+ ?/', $search_expression, $matches);
print_r($matches[0]);
Yields:
Array
(
[0] => +word1
[1] => -word2
[2] => -"word word"
)
Se Example.

Split string on semicolons immediately followed by 7 digital characters

I have a string like this:
2234323,23,23,44,433;3,23,44,433;23,23,44,433;23,23,44,433;4534453,23,23,44,433;
3,23,44,433;23,23,44,433;23,23,44,433
7545455,23,23,44,433;3,23,44,433;23,23,44,433;23,23,44,433
As you see, there are semicolons between values. I want to split this string based on 'only semicolons before 7 digits values' so I should have this:
>2234323,23,23,44,433;3,23,44,433;23,23,44,433;23,23,44,433
>4534453,23,23,44,433;3,23,44,433;23,23,44,433;23,23,44,433;
>7545455,23,23,44,433;3,23,44,433;23,23,44,433;23,23,44,433
the only thing that I can think of is explode(';',$string) but this returns this:
>2234323,23,23,44,433;
>3,23,44,433;
>23,23,44,433;
>23,23,44,433
>4534453,23,23,44,433;
>3,23,44,433;
>23,23,44,433;23,23,44,433;
>7545455,23,23,44,433;
>3,23,44,433;23,23,44,433;
>23,23,44,433
Is there any fast method to split string with this format based on the ";" before 7 digits values?
You can use preg_split for that:
$s = '2234323,23,23,44,433;3,23,44,433;23,23,44,433;23,23,44,433;4534453,23,23,44,433;3,23,44,433;23,23,44,433;23,23,44,433;7545455,23,23,44,433;3,23,44,433;23,23,44,433;23,23,44,433';
var_dump(preg_split('/(;\d{7},)/', $s, -1, PREG_SPLIT_DELIM_CAPTURE));
Your output will be
array(5) {
[0] =>
string(58) "2234323,23,23,44,433;3,23,44,433;23,23,44,433;23,23,44,433"
[1] =>
string(9) ";4534453,"
[2] =>
string(50) "23,23,44,433;3,23,44,433;23,23,44,433;23,23,44,433"
[3] =>
string(9) ";7545455,"
[4] =>
string(50) "23,23,44,433;3,23,44,433;23,23,44,433;23,23,44,433"
}
I think that the next thing (combine the 1st and 2nd and then 3rd and 4th elements) is not a big deal :)
Let me know if you still here problems here.
You could do a find and replace on numbers that are seven digits long, to insert a token that you can use to split. The output may need a little extra filtering to get to your desired format.
<?php
$in =<<<IN
2234323,23,23,44,433;3,23,44,433;23,23,44,433;23,23,44,433;4534453,23,23,44,433;
3,23,44,433;23,23,44,433;23,23,44,433
7545455,23,23,44,433;3,23,44,433;23,23,44,433;23,23,44,433
IN;
$out = preg_replace('/([0-9]{7})/', "#$1", $in);
$out = explode('#', $out);
$out = array_filter($out);
var_export($out);
Output:
array (
1 => '2234323,23,23,44,433;3,23,44,433;23,23,44,433;23,23,44,433;',
2 => '4534453,23,23,44,433;
3,23,44,433;23,23,44,433;23,23,44,433
',
3 => '7545455,23,23,44,433;3,23,44,433;23,23,44,433;23,23,44,433',
)
Your input structure seems a little unstable, but once it is stabilized, just use preg_split() to match (and consume) semicolons that are immediately followed by exactly 7 digits. \b is a word boundary to ensure that their is no 8th digit.
Code: (Demo)
$string = <<<STR
2234323,23,23,44,433;3,23,44,433;23,23,44,433;23,23,44,433;4534453,23,23,44,433;
3,23,44,433;23,23,44,433;23,23,44,433
7545455,23,23,44,433;3,23,44,433;23,23,44,433;23,23,44,433
STR;
$string = preg_replace('/;?\R/', ';', $string); // I don't know if this is actually necessary for your real project
var_export(
preg_split('/;(?=\d{7}\b)/', $string)
);
Output:
array (
0 => '2234323,23,23,44,433;3,23,44,433;23,23,44,433;23,23,44,433',
1 => '4534453,23,23,44,433;3,23,44,433;23,23,44,433;23,23,44,433',
2 => '7545455,23,23,44,433;3,23,44,433;23,23,44,433;23,23,44,433',
)

How to extract substrings with delimiters from a string in php

I would like to remove substrings from a string that have delimiters.
Example:
$string = "Hi, I want to buy an [apple] and a [banana].";
How do I get "apple" and "banana" out of this string and in an array? And the other parts of the string "Hi, I want to buy an" and "and a" in another array.
I apologize if this question has already been answered. I searched this site and couldn't find anything that would help me. Every situation was just a little different.
You could use preg_split() thus:
<?php
$pattern = '/[\[\]]/'; // Split on either [ or ]
$string = "Hi, I want to buy an [apple] and a [banana].";
echo print_r(preg_split($pattern, $string), true);
which outputs:
Array
(
[0] => Hi, I want to buy an
[1] => apple
[2] => and a
[3] => banana
[4] => .
)
You can trim the whitespace if you like and/or ignore the final fullstop.
preg_match_all('(?<=\[)([a-z])*(?=\])', $string, $matches);
Should do what you want. $matches will be an array with each match.
I assume you want words as values in the array:
$words = explode(' ', $string);
$result = preg_grep('/\[[^\]]+\]/', $words);
$others = array_diff($words, $result);
Create an array of words using explode() on a space
Use a regex to find [somethings] using preg_grep()
Find the difference of all words and [somethings] using array_diff(), which will be the "other" parts of the string

Splitting strings in to array by intergers in the string using php

I have this example string.
$string = "There four of them. These are: 1 The first one. Its is the most common. 2 The second one. 3 The third one. 4 This is the last.";
I wanted to split into array containing information given in the above $string. I want it to look like this.
Array (
[0] => The first one. Its is the most common.
[1] => The second one.
[2] => The third one.
[3] => This is the last.
)
Can any help me with it. Thank you.
You can use preg_split to split the string by integers, for example:
$string = "There four of them. These are: 1 The first one. Its is the most common. 2 The second one. 3 The third one. 4 This is the last.";
$matches = preg_split('/\d+/', $string);
var_dump($matches);
You can use regex in preg_match_all() to select target part of string. The regex select string between two digits.
preg_match_all("/\d+([^\d]+)/", $string, $matches);
print_r($matches[1]);
See result in demo

Php Regex: isolate and count occurrences

So far I managed to isolate and count pictures in a string by doing this:
preg_match_all('#<img([^<]+)>#', $string, $temp_img);
$count=count($temp_img[1]);
I would like to do something similar with parts that would look like this:
"code=mYrAnd0mc0dE123".
For instance, let's say I have this string:
$string="my first code is code=aZeRtY and my second one is code=qSdF1E"
I would like to store "aZeRtY" and "qSdF1E" in an array.
I tried a bunch of regex to isolate the "code=..." but none has worked for me.
Obviously, regex is beyond me.
Are you looking for this?
preg_match_all('#code=([A-Za-z0-9]+)#', $string, $results);
$count = count($results[1]);
This:
$string = '
code=jhb2345jhbv2345ljhb2435
code=jhb2345jhbv2345ljhb2435
code=jhb2345jhbv2345ljhb2435
code=jhb2345jhbv2345ljhb2435
';
preg_match_all('/(?<=code=)[a-zA-Z0-9]+/', $string, $matches);
echo('<pre>');
print_r($matches);
echo('</pre>');
Outputs:
Array
(
[0] => Array
(
[0] => jhb2345jhbv2345ljhb2435
[1] => jhb2345jhbv2345ljhb2435
[2] => jhb2345jhbv2345ljhb2435
[3] => jhb2345jhbv2345ljhb2435
)
)
However without a suffixing delimiter, it won't work correctly if this pattern is concatenated, eg: code=jhb2345jhbv2345ljhb2435code=jhb2345jhbv2345ljhb2435
But perhaps that won't be a problem for you.

Categories