Print out certain text from string with preg_match - php

I want to get a certain string from an .torrent name but I'm only getting this from it:
array
0 => string 'e' (length=1)
What have I done wrong? This is the preg_match I use:
preg_match('/[S(0-9)E(0-9)]/i', 'True.Blood.S04E12.SWESUB.PDTV.XviD-DSMEDiA', $matches);
Thanks in advance.

Remove the square brackets and put them around the numbers and add + (meaning 1 or more) after them. This way you get the entire S##E## string, plus the numbers separately:
preg_match('/S([0-9]+)E([0-9]+)/i', 'True.Blood.S04E12.SWESUB.PDTV.XviD-DSMEDiA', $matches);
print_r($matches);
/* output:
Array
(
[0] => S04E12
[1] => 04
[2] => 12
)
*/
You could also replace [0-9] with \d

I would recommend using:
preg_match('/S[0-9]{1,2}E[0-9]{1,2}/i', 'True.Blood.S04E12.SWESUB.PDTV.XviD-DSMEDiA', $matches);
It gets out this:
array(1) { [0]=> string(6) "S04E12" }

The following regex will return the appropriate string.
/* Pattern: /\d{2}E\d{2}/ */
preg_match_all('/\d{2}E \d{2}/', '{{your data}}', $arr, PREG_PATTERN_ORDER);
/*Result*/
Array
(
[0] => Array
(
[0] => 04E12
)
)

Related

Divide PHP string into arrayed substrings based on "divider" characters

What I'd like to do is split a PHP string into a set of sub-strings grouped into arrays based on "divider" characters that begin those sub-strings. The characters *, ^, and % are reserved as divider characters. So if I have the string "*Here's some text^that is meant*to be separated^based on where%the divider characters^are", it should be split up and placed in arrays like so:
array(2) {
[0] => "*Here's some text"
[1] => "*to be separated"
}
array(3) {
[0] => "^that is meant"
[1] => "^based on where"
[2] => "^are"
}
array(1) {
[0] => "%the divider characters"
}
I'm totally lost on this one. Does anyone know how to implement this?
You don't ask for $matches[0] so unset it if you want:
preg_match_all('/(\*[^*^%]+)|(\^[^*^%]+)|(%[^*^%]+)/', $string, $matches);
$matches = array_map('array_filter', $matches);
print_r($matches);
The array_filter() removes the empties from the capture group sub-arrays to give the array shown in the question

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 )

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>';

splitting name and id

I have a string
"JonSmith02-11-1955"
I use
preg_split to get JonSmith (reg='[0-9-]') then do it again ('[a-zA-Z]\ ') to get his birthdate.
Is there any better way to get them both in one split ?
/(?<=[a-z])(?=\d)/i
works in this case. It matches a position preceded by a letter and followed by a digit. Read about lookbehinds and -aheads for more information.
This won't work if the name can contain digits.
DEMO
What about ^(.*)(\d{2})\-(\d{2})\-(\d{4})$?
It will parse your string into four parts: JonSmith, 02, 11 and 1955.
Try /(\w*)(\d{2}-\d{2}-\d{4})/ as your regex - someone else can probably do that more efficiently. This will give you two capturing groups, so
$array = preg_match_all('/(\w*)(\d{2}-\d{2}-\d{4})/', "JonSmith02-11-1955");
print_r($array);
/*
Array
(
[0] => Array
(
[0] => JonSmith02-11-1955
)
[1] => Array
(
[0] => JonSmith
)
[2] => Array
(
[0] => 02-11-1955
)
)
*/
^([a-zA-z]*)([0-9]*)\-(.+)*$
For this example :
JonSmith02-11-1955
Give :
JonSmith
02
11-1955
preg_match('/([a-z]+)([0-9-]+)/i', 'JonSmith02-11-1955', $matches);
echo "Name is $matches[1]<br>\n";
echo "Birth date is $matches[2]<br>\n";
See it working

Categories