Sorry for dropping an other preg_match regular expression question. I have been dealing with this issue for too long and can't get it to work, so any help is appreciated!!
I need a simple preg_match_all to return me placeholder for the format [#varname].
in a string
"hello, this is [#firstname], i am [#age] years old"
I would expect an array with the values 'firstname' and 'age'.
my attempt for [varname] that worked was:
preg_match_all("/[([^]]+)]/", $t, $result);
anything i tried to get the #character included failed...
preg_match_all("/[\#([^]]+)]/", $t, $result);
Code:
$str = 'hello, this is [#firstname], i am [#age] years old';
preg_match_all('~\[#(.+?)\]~', $str, $matches);
print_r($matches);
Output:
Array
(
[0] => Array
(
[0] => [#firstname]
[1] => [#age]
)
[1] => Array
(
[0] => firstname
[1] => age
)
)
<?php
$string = "hello, this is [#firstname], i am [#age] years old";
$pattern = '#\[#(.*?)\]#';
preg_match_all($pattern, $string, $matches);
print_r($matches[1]);
?>
Array
(
[0] => firstname
[1] => age
)
Related
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
)
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.
This question already has answers here:
Preg_match_all returning array within array?
(2 answers)
Closed 8 years ago.
I have this:
$text = $_POST['text'];
When I echo $text I get this:
ggg #hhh #ddd ggg hhhrr ggg #ttt
When I do this:
$val = preg_match_all("/#\w+/", $text, $matches);
print_r($matches);
I get
Array ( [0] => Array ( [0] => #hhh [1] => #ddd [2] => #ttt ) )
But I want output like this:
Array ( [0] => #hhh [1] => #ddd [2] => #ttt )
thanks
Another approach is to use named groups.
$val = preg_match_all("/(?P<myLabel>#\w+)/", $text, $matches);
print_r($matches['myLabel']);
Take the first (zeroth) item from the array by changing this:
print_r($matches);
To this:
print_r($matches[0]);
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;
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 /{.*?}.*?{\/.*?}/