Currently I have this regexp to detect strings between double curly brackets and it work's wonderfully.
$str = "{{test}} and {{test2}}";
preg_match_all('/(?<={{)[^}]*(?=}})/', $str, $matches);
print_r($matches);
Returns:
Array
(
[0] => Array
(
[0] => test
[1] => test2
)
)
Now I need to expand it to only match stuff between ]] and [[
$str = "{{dont match}}]]{{test}} and {{test2}}[[{{dont match}}";
I've been trying to modify the regex but the lookahead and lookbehind is making it too difficult for me. How can I get it to match stuff inside ]] and [[ only?
Also I would like to match the whole string between ]] and [[ and then I would like to match each individual string between {{ }} inside it.
For example:
$str = "{{dont match}}]]{{test}} and {{test2}}[[{{dont match}}";
Would return:
Array
(
[0] => Array
(
[0] => {{test}} and {{test2}}
[1] => test
[2] => test2
)
)
Piggyback using preg_replace_callback:
$str = "{{dont match}}]]{{test}} and {{test2}}[[{{dont match}}";
$arr = array();
preg_replace_callback('/\]\](.*?)\[\[/', function($m) use (&$arr) {
preg_match_all('/(?<={{)[^}]*(?=}})/', $m[1], $arr); return true; }, $str);
print_r($arr[0]);
Output:
Array
(
[0] => test
[1] => test2
)
Related
$HTML:
{list:start:Data}
{id} is having a title of {title}
{list:end:Data}
Data is dynamic and could be any string.
I am trying to loop all the occurences with the following code:
preg_match_all('/\{list:start:(.*?)\}(.*?)\{list:end:(.*?)\}/', $HTML, $match);
I want the following result:
$match = array(
array(
"string" => "Data",
"value" => "{id} is having a title of {title}"
)
);
but I get the follow result:
$match = array(
[0] => Array
(
)
[1] => Array
(
)
[2] => Array
(
)
[3] => Array
(
)
);
but that isn't working as $match returns an empty array. After a few hour of searching for a solution I am still no closer to a working result.
As an alternative, you can make use of a negated character class instead of using .*? with the /s modifier to have the dot match a newline.
If you don't want to match consecutive lines that start with {list: you can use a negative lookahead rules out those matches.
^{list:start:([^}]+)}\R((?:(?!{list:).*\R)*+){list:end:[^}]+}
The pattern matches:
^ Start of string
{list:start: Match literally (Note that the { does not need to be escaped)
( Capture group 1
[^}]+ Match 1+ times any char except }
) Close group 1
} Match the closing }
\R match any unicode newline sequence
( Capture group 2
(?:(?!{list:).*\R)*+ Repeat matching all lines as long as they not start with list:
) Close group 2
{list:end: Match literally
[^}]+ Match 1+ times any char except }
} Match the closing }
See a regex demo and a Php demo.
Example code
$re = '/^{list:start:([^}]+)}\R((?:(?!{list:).*\R)*){list:end:[^}]+}/m';
$str = '{list:start:Data}
{id} is having a title of {title}
{list:end:Data}
{list:start:Data}
{list:start:Data}
{id} is having a title of {title}
this is some text
{list:end:Data}';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
print_r(array_map(function($x){
return [
"string" => $x[1],
"data" => trim($x[2])
];
}, $matches));
Output
Array
(
[0] => Array
(
[string] => Data
[data] => {id} is having a title of {title}
)
[1] => Array
(
[string] => Data
[data] => {id} is having a title of {title}
this is some text
)
)
You need to escape the braces, and use /s to match more than single line. Below is the code example.
Code
<?php
$input = '
{list:start:Data}
{id} is having a title of {title}
{list:end:Data}
{list:start:Data1}
{id1} is having a title of {title1}
{list:end:Data1}
{list:start:Data2}
{id2} is having a title of {title2}
{list:end:Data2}
{list:start:Data3}
{id3} is having a title of {title3}
{list:end:Data3}
';
preg_match_all(
"/\\{list:start:(.+?)\\}(.*?)\\{list:end:(.+?)\\}/s",
$input,
$preg_matches
);
$matches = [];
foreach ($preg_matches[1] as $k => $v) {
$matches[] = [
"string" => trim($v),
"data" => trim($preg_matches[2][$k])
];
}
print_r($matches);
Output
Array
(
[0] => Array
(
[string] => Data
[data] => {id} is having a title of {title}
)
[1] => Array
(
[string] => Data1
[data] => {id1} is having a title of {title1}
)
[2] => Array
(
[string] => Data2
[data] => {id2} is having a title of {title2}
)
[3] => Array
(
[string] => Data3
[data] => {id3} is having a title of {title3}
)
)
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.
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>';
for example i have sentenes like this:
$text = "word, word w.d. word!..";
I need array like this
Array
(
[0] => word
[1] => word
[2] => w.d
[3] => word".
)
I am very new for regular expression..
Here is what I tried:
function divide_a_sentence_into_words($text){
return preg_split('/(?<=[\s])(?<!f\s)\s+/ix', $text, -1, PREG_SPLIT_NO_EMPTY);
}
this
$text = "word word, w.d. word!..";
$split = preg_split("/[^\w]*([\s]+[^\w]*|$)/", $text, -1, PREG_SPLIT_NO_EMPTY);
print_r($split);
works, but i have second question i want to write list in mu regular exppression
"w.d" is special case.. for example this words is my list "w.d" , "mr.", "dr."
if i will take text:
$text = "word, dr. word w.d. word!..";
i need array:
Array (
[0] => word
[1] => dr.
[2] => word
[3] => w.d
[4] => word
)
sorry for bad english...
Using preg_split with a regex of /[^\w]*([\s]+[^\w]*|$)/ should work fine:
<?php
$text = "word word w.d. word!..";
$split = preg_split("/[^\w]*([\s]+[^\w]*|$)/", $text, -1, PREG_SPLIT_NO_EMPTY);
print_r($split);
?>
DEMO
Output:
Array
(
[0] => word
[1] => word
[2] => w.d
[3] => word
)
Use the function explode, that will split the string into an array
$words = explode(" ", $text);
use
str_word_count ( string $string [, int $format = 0 [, string $charlist ]] )
see here http://php.net/manual/en/function.str-word-count.php
it does exactly what you want. So in your case :
$myarray = str_word_count ($text,1);
I need to save the number between every pair of curly brackets as a variable.
{2343} -> $number
echo $number;
Output = 2343
I don't know how to do the '->' part.
I've found a similar function, but it simply removes the curly brackets and does nothing else.
preg_replace('#{([0-9]+)}#','$1', $string);
Is there any function I can use?
You'll probably want to use preg_match with a capture:
$subject = "{2343}";
$pattern = '/\{(\d+)\}/';
preg_match($pattern, $subject, $matches);
print_r($matches);
Output:
Array
(
[0] => {2343}
[1] => 2343
)
The $matches array will contain the result at index 1 if it is found, so:
if(!empty($matches) && isset($matches[1)){
$number = $matches[1];
}
If your input string can contain many numbers, then use preg_match_all:
$subject = "{123} {456}";
$pattern = '/\{(\d+)\}/';
preg_match_all($pattern, $subject, $matches);
print_r($matches);
Output:
Array
(
[0] => Array
(
[0] => {123}
[1] => {456}
)
[1] => Array
(
[0] => 123
[1] => 456
)
)
$string = '{1234}';
preg_replace('#{([0-9]+)}#e','$number = $1;', $string);
echo $number;