How to extract substrings with delimiters from a string in php - 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

Related

Php splitting a sentence

I'm trying to split a string of sentences by "." to get each sentence in an array. Like below:
$Text = "Hello, Mr. James. How are you today."
$split= explode(".", $Text);
As you can see $Text contains 2 sentences therefore i should only have 2 elements in the array. The issue i'm having is that sometimes my $Text can contain words like "Mr." or any other word which contains a "." in the middle of a sentence. This will result in the sentences being split from the middle and placed separately in the array like below:
Array ( [0] => Hello, Mr [1] => James [2] => How are you today [3] => )
You can avoid a lot of exception handling and general misery, if you can ensure that all English sentences are properly spaced at the end of each sentence -- 2 consecutive spaces. This can be difficult when dealing with some digitized strings because sometimes multi-spacing gets condensed to a single space.
This is what I mean:
$Text = "Hello, Mr. James. How are you today.";
$split = explode(" ", $Text);
var_export($split);
// array ( 0 => 'Hello, Mr. James.', 1 => 'How are you today.', )
Exploding on each space-space will give you a reliable result.
If you want good output, you'll need to use good input.
If you want to blacklist a few predictable substrings that should not be use to split the string, then you can use (*SKIP)(*FAIL) for that.
Code: (Demo)
$text = "Hello, Mr. James. How are you today.";
var_export(
preg_split('~(?:Mrs?|Miss|Ms|Prof|Rev|Col|Dr)[.?!:](*SKIP)(*F)|[.?!:]+\K\s+~', $text, 0, PREG_SPLIT_NO_EMPTY)
);
Output:
array (
0 => 'Hello, Mr. James.',
1 => 'How are you today.',
)

Split string into array by regular expression without bracket

My string variable contains "[100][200][300][400]" data.
This variable should be split it into array without brackets.
Currently I can split into $matches array with regular expression, but bracket appear in array.
I have used current expression as bellow:
preg _match_all('/\[.*?\]/', $string , $matches)
why not try this:
<?php
$str = " [100][200][300][400] ";
$str = explode("][", trim($str, "[] "));
print_r($str);
exit;
This code maybe can help
$str = "[100][200][300][400]";
$str = explode("][",trim($str,'\[\]'));
Results in:
Array(
[0] => 100
[1] => 200
[2] => 300
[3] => 400
)
Use brackets to choose what part of the match you want to return.
preg_match_all('/\[(.*?)\]/', $string , $matches)

How to split a string into an array using a given regex expression

I am trying to explode / preg_split a string so that I get an array of all the values that are enclosed in ( ). I've tried the following code but I always get an empty array, I have tried many things but I cant seem to do it right
Could anyone spot what am I missing to get my desired output?
$pattern = "/^\(.*\)$/";
$string = "(y3,x3),(r4,t4)";
$output = preg_split($pattern, $string);
print_r($output);
Current output Array ( [0] => [1] => )
Desired output Array ( [0] => "(y3,x3)," [1] => "(r4,t4)" )
With preg_split() your regex should be matching the delimiters within the string to split the string into an array. Your regex is currently matching the values, and for that, you can use preg_match_all(), like so:
$pattern = "/\(.*?\)/";
$string = "(y3,x3),(r4,t4)";
preg_match_all($pattern, $string, $output);
print_r($output[0]);
This outputs:
Array
(
[0] => (y3,x3)
[1] => (r4,t4)
)
If you want to use preg_split(), you would want to match the , between ),(, but without consuming the parenthesis, like so:
$pattern = "/(?<=\)),(?=\()/";
$string = "(y3,x3),(r4,t4)";
$output = preg_split($pattern, $string);
print_r($output);
This uses a positive lookbehind and positive lookahead to find the , between the two parenthesis groups, and split on them. It also output the same as the above.
You can use a simple regex like \B,\B to split the string and improve the performance by avoiding lookahead or lookbehind regex.
\B is a non-word boundary so it will match only the , between ) and (
Here is a working example:
http://regex101.com/r/cV7bO7/1
$pattern = "/\B,\B/";
$string = "(y3,x3),(r4,t4),(r5,t5)";
$result = preg_split($pattern, $string);
$result will contain:
Array
(
[0] => (y3,x3)
[1] => (r4,t4)
[2] => (r5,t5)
)

split string by any amount of whitespace in PHP

I know how to split a string so the words between the delimitor into elements in an array using .explode() by " ".
But that only splits the string by a single whitespace character. How can I split by any amount of whitespace?
So an element in the array end when whitespace is found and the next element in the array starts when the first next non-whitespace character is found.
So something like "The quick brown fox" turns into an array with The, quick, brown, and fox are elements in the returned array.
And "jumped over the lazy dog" also splits so each word is an individual element in the returned array.
Like this:
preg_split('#\s+#', $string, null, PREG_SPLIT_NO_EMPTY);
$yourSplitArray=preg_split('/[\ \n\,]+/', $your_string);
try this
preg_split(" +", "hypertext language programming"); //for one or more whitespaces
you can see here: PHP explode() Function
<?php
$str = "Hello world. It's a beautiful day.";
print_r (explode(" ",$str));
?>
will return:
Array ( [0] => Hello [1] => world. [2] => It's [3] => a [4] => beautiful [5] => day. )

Preg_split or preg_match

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 "-"

Categories