This question already has answers here:
preg match count matches
(5 answers)
Closed 6 years ago.
I have a string like this:
$str = 'this is a string';
And this is my pattern: /i/g. There is three occurrence (as you see in the string above, it is containing three i). Now I need to count that. How can I get that number?
you can use substr_count() as well as preg_match_all()
echo substr_count("this is a string", "i"); // will echo 3
echo $k_count = preg_match_all('/i/i', 'this is a string', $out); // will echo 3
other method is convert into array and then count it:
$arr = str_split('this is a string');
$counts = array_count_values($arr);
print_r($counts);
output:
Array
(
[t] => 2
[h] => 1
[i] => 3
[s] => 3
[ ] => 3
[a] => 1
[r] => 1
[n] => 1
[g] => 1
)
You should use substr_count().
$str = 'this is a string';
echo substr_count($str, "i"); // 3
You can also use mb_substr_count()
$str = 'this is a string';
echo mb_substr_count($str, "i"); // 3
substr_count — Count the number of substring occurrences
mb_substr_count — Count the number of substring occurrences
preg_match_all could be a better fit.
Here is an example:
<?php
$subject = "a test string a";
$pattern = '/a/i';
preg_match_all($pattern, $subject, $matches);
print_r($matches);
?>
Prints:
Array ( [0] => Array ( [0] => a [1] => a ) )
Related
This question already has answers here:
preg_split() String into Text and Numbers [duplicate]
(2 answers)
Closed 4 years ago.
Sorry for my bad English, I would to ask about How to split characters and numbers using preg_split(). For example, I have any data with prefix like :
ABC00001 to array(0 => 'ABC', 1 => 00001)
DEFG00002 to array(0 => 'DEFG', 1 => 00002)
AB00003 to array(0 => 'AB', 1 => 00003)
Thanks for advise
Split on the zero-length position that follows the sequence of letters.
Code: Demo
$string = 'ABC00001';
$output = preg_split('~[A-Z]+\K~', $string);
var_export($output);
The \K says forget the previous matched characters.
You can use preg_split with the PREG_SPLIT_DELIM_CAPTURE and PREG_SPLIT_NO_EMPTY flags to do what you want:
$string = 'ABC00001';
$output = preg_split('/([A-Z]+)/', $string, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
print_r($output);
Output:
Array ( [0] => ABC [1] => 00001 )
Demo on 3v4l.org
I'm using a regex to check a string for a certain format.
preg_match('/^\/answer ([1-9][0-9]*) (.{1,512})$/', $str, $hit, PREG_OFFSET_CAPTURE);
Using this regex, the posted string needs to have this format:
/answer n x
n -> an integer > 0
x -> a string, 512 chars maximum
Now how to extract "n" and "x" the easiest way using PHP?
For example:
/answer 56 this is my sample text
Should lead to:
$value1 = 56;
$value2 = "this is my sample text";
Running this simple piece of code
<?php
$hit = [];
$str = '/answer 56 this is my sample text';
preg_match('/^\/answer ([1-9][0-9]*) (.{1,512})$/', $str, $hit, PREG_OFFSET_CAPTURE);
echo'<pre>',print_r($hit),'</pre>';
Will show you, that $hit has following values:
<pre>Array
(
[0] => Array
(
[0] => /answer 56 this is my sample text
[1] => 0
)
[1] => Array
(
[0] => 56
[1] => 8
)
[2] => Array
(
[0] => this is my sample text
[1] => 11
)
)
1</pre>
Here:
$hit[0][0] is full string that matches your pattern
$hit[1][0] is a substring that matches pattern [1-9][0-9]*
$hit[2][0] is a substring that matches pattern .{1,512}
So,
$value1 = $hit[1][0];
$value2 = $hit[2][0];
using php
I'm having a problem , I wanna read a text and separate it in numbers and letters
for example I wanna read this text: 4FS+2d,14 and get this output:'4' 'FS' '+' '2' 'd' ',' '14'
any idea how to do that?
thanks
Just use preg_match_all.
$string = '4FS+2d,14';
preg_match_all('/[0-9]+|[a-zA-Z]+|[^0-9a-zA-Z]+/', $string, $matches);
// ^ digits
// ^ chars
// ^ not digits, not chars
echo json_encode($matches); // json_encode for readability
// [["4","FS","+","2","d",",","14"]]
[0-9]+|[a-zA-Z]+|[^0-9a-zA-Z]+
Debuggex Demo
$str = '4FS+2d,14';
$arr = preg_split('#([\W]+|[\d]+)#i', $str, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
print_r($arr);
exit;
// output
Array
(
[0] => 4
[1] => FS
[2] => +
[3] => 2
[4] => d
[5] => ,
[6] => 14
)
Try like this:
<?php
$str = "4FS+2d,14";
for($loop=0;$loop<strlen($str);$loop++) {
$len[] = $str[$loop];
}
print_r($len);
?>
demo link: http://phpfiddle.org/main/code/de8-4yp
This question already has answers here:
Explode string by one or more spaces or tabs
(8 answers)
Closed 7 months ago.
With the following string:
$str = '["one","two"],a,["three","four"],a,,a,["five","six"]';
preg_split( delimiter pattern, $str );
How would I have to set up the delimiter pattern to obtain this result:
$arr[0] = '["one","two"]';
$arr[1] = '["three","four"]';
$arr[2] = '["five","six"]';
In other words, is there a way to split at the pattern ',a,' AND ',a,,a,' BUT check for ',a,,a,' first because ',a,' is a sub string of ',a,,a,'?
Thanks in advance!
If it can only be ,a, and ,a,,a,, then this should be enough:
preg_split("/(,a,)+/", $str);
It looks like what you're actually trying to do is separate out the square bracketed parts. You could do that like so:
$arr = preg_split("/(?<=\])[^[]*(?=\[)/",$str);
Take a look at this code:
$result = array();
preg_match_all("/(\[[^\]]*\])/", '["one","two"],a,["three","four"],a,,a,["five","six"]', $result);
echo '<pre>' . print_r($result, true);
It will return:
Array
(
[0] => Array
(
[0] => ["one","two"]
[1] => ["three","four"]
[2] => ["five","six"]
)
[1] => Array
(
[0] => ["one","two"]
[1] => ["three","four"]
[2] => ["five","six"]
)
)
I'm creating regular expression in the form: A | B | C ... automatically, by program,
where A, B, C, ... are constant strings.
I need to find all the matches that correspond to these regular expression,
even if the A, B, C, ... have not empty intersection, or someone is substring of other.
Example:
preg_match_all ('/Hello World|Hello|World lo/i', 'xxxHello worldxxx', $m, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
var_export ($m);
It gives:
array (
0 =>
array (
0 =>
array (
0 => 'Hello World'
1 => 3, // start of match
)
)
)
I would need:
array (
0 =>
array (
0 =>
array (
0 => 'Hello World'
1 => 3, // start of match
)
1 =>
array (
0 => 'Hello'
1 => 3, // start of match
)
2 =>
array (
0 => 'lo world'
1 => 6, // start of match
)
)
)
Is there any way to get it?
Thanks
Run a preg_match_all for each expression.
I would use strpos:
$str = 'xxxHello worldxxx';
$arr = array('Hello World', 'Hello', 'World');
foreach($arr as $word) {
$pos = strpos(strtolower($str), strtolower($word));
echo "$word found at char $pos\n";
}
output:
Hello World found at char 3
Hello found at char 3
World found at char 9