Splitting a string in certain way - php

I have the following string
{item1}home::::Home{/item1}{item2}contact_us::::Contact Us{/item2}{item3}.....
and so it goes on.
I need to split the string the following way
1=>{item1}home::::Home{/item1}
2=>{item2}contact_us::::Contact Us{/item2}
Is there a way?

$input = '{item1}home::::Home{/item1}{item2}contact_us::::Contact Us{/item2}{item3}.....';
$regex = '/{(\w+)}.*{\/\1}/';
preg_match_all($regex, $input, $matches);
print_r($matches[0]);

You could do it like this:
$text = "{item1}home::::Home{/item1}{item2}contact_us::::Contact Us{/item2}{item3}.....){/item3}";
preg_match_all('/{item\d}.+?{\/item\d}/', $text, $results);
var_dump($results) would produce:
Array
(
[0] => Array
(
[0] => {item1}home::::Home{/item1}
[1] => {item2}contact_us::::Contact Us{/item2}
[2] => {item3}.....){/item3}
)
)

Use preg_split() with the regex pattern /{.*?}.*?{\/.*?}/

Related

PHP preg_split. Can't find or figure out ex for this

I did try but can't find or figure out regex for the following
Working in PHP need preg_split to work
{password=123456, telephone=9452979342}/{Accept=application/json, Authorization=Bearer kadaljdlkadjsdaskdjaskdasdf}
What I want is to split this string into array like this:
Split by '/' in middle
[0] => {password=123456, telephone=9452979342}
[2] => {Accept=application/json, Authorization=Bearer kadaljdlkadjsdaskdjaskdasdf}
Select between {*}
[0] => password=123456, telephone=9452979342
[2] => Accept=application/json, Authorization=Bearer kadaljdlkadjsdaskdjaskdasdf
As per your way , you may try the following approach as well
$re = '/\}\/\{/m';
$str = '{password=123456, telephone=9452979342}/{Accept=application/json, Authorization=Bearer kadaljdlkadjsdaskdjaskdasdf}';
$arr=preg_split($re,$str);
$cnt=0;
foreach($arr as $values)
$arr[$cnt++]=preg_replace('/[{}]/','',$values);
print_r($arr);
run here
Is that what you want:
$str = "{password=123456, telephone=9452979342}/{Accept=application/json, Authorization=Bearer kadaljdlkadjsdaskdjaskdasdf}";
preg_match('~\{(.+?)\}/\{(.+?)\}~', $str, $matches);
print_r($matches);
Output:
Array
(
[0] => {password=123456, telephone=9452979342}/{Accept=application/json, Authorization=Bearer kadaljdlkadjsdaskdjaskdasdf}
[1] => password=123456, telephone=9452979342
[2] => Accept=application/json, Authorization=Bearer kadaljdlkadjsdaskdjaskdasdf
)

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 )

Custom regular expression pattern

What's the right pattern to obtain something like that using preg_split.
Input:
Src.[VALUE1] + abs(Src.[VALUE2])
Output:
Array (
[0] => Src.[VALUE1]
[1] => Src.[VALUE2]
)
Instead of using preg_split, using preg_match_all makes more sense in this case:
preg_match_all('/\w+\.\[\w+\]/', $str, $matches);
$matches = $matches[0];
Result of $matches:
Array
(
[0] => Src.[VALUE1]
[1] => Src.[VALUE2]
)
This regex should be fine
Src\.\[[^\]]+\]
But instead of preg_split I'd suggest using preg_match_all
$string = 'Src.[VALUE1] + abs(Src.[VALUE2])';
$matches = array();
preg_match_all('/Src\.\[[^\]]+\]/', $string, $matches);
All matches you're looking for will be bound to $matches[0] array.
I guess preg_match_all is what you want. This works -
$string = "Src.[VALUE1] + abs(Src.[VALUE2])";
$regex = "/Src\.\[.*?\]/";
preg_match_all($regex, $string, $matches);
var_dump($matches[0]);
/*
OUTPUT
*/
array
0 => string 'Src.[VALUE1]' (length=12)
1 => string 'Src.[VALUE2]' (length=12)

Finding the no of occurence of a string inside another string using regex in PHP?

I want to find the no of occurences of a sustring(pattern based) inside another string.
For example:
$mystring = "|graboard='KERALA'||graboarded='KUSAT'||graboard='MG'";
I want to find the no of graboards present in the $mystring,
So I used the regex for this, But how will I find the no of occurrence?
If you must use a regex, preg_match_all() returns the number of matches.
Use preg_match_all:
$mystring = "|graboard='KERALA'||graboarded='KUSAT'||graboard='MG'";
preg_match_all("/(graboard)='(.+?)'/i", $mystring, $matches);
print_r($matches);
will yield:
Array
(
[0] => Array
(
[0] => graboard='KERALA'
[1] => graboard='MG'
)
[1] => Array
(
[0] => graboard
[1] => graboard
)
[2] => Array
(
[0] => KERALA
[1] => MG
)
)
So then you can use count($matches[1]) -- however, this regex may need to be modified to suit your needs, but this is just a basic example.
Just use preg_match_all():
// The string.
$mystring="|graboard='KERALA'||graboarded='KUSAT'||graboard='MG'";
// The `preg_match_all()`.
preg_match_all('/graboard/is', $mystring, $matches);
// Echo the count of `$matches` generated by `preg_match_all()`.
echo count($matches[0]);
// Dumping the content of `$matches` for verification.
echo '<pre>';
print_r($matches);
echo '</pre>';

How to split a string at the first colon

$str = "X-Storage-Url: https://pathofanapi";
I would like to split this into an array ("X-Storage-Url", "https://pathofanapi").
Could someone tell me the regex for this ? Regex has always been my weakness.
Thanks.
$array = array_map('trim', explode(':', $str, 2));
As it's been said, explode is the right tool to do this job.
However, if you really want a regex, here is a way to do:
with preg_match:
$str = "X-Storage-Url: https://pathofanapi";
preg_match('/^([^:]+):\s*(.*)$/', $str, $m);
print_r($m);
output:
Array
(
[0] => X-Storage-Url: https://pathofanapi
[1] => X-Storage-Url
[2] => https://pathofanapi
)
or with preg_split;
$arr = preg_split('/:\s*/', $str, 2);
print_r($arr);
output:
Array
(
[0] => X-Storage-Url
[1] => https://pathofanapi
)

Categories