This question already has answers here:
PHP Regular Expression: string from inside brackets
(3 answers)
Closed 5 years ago.
My string:
fields[name_1]
I want to get fields and name_1 using regex.
I'm know about preg_match_all(), but I'm not friends with regular expressions.
This can be used for direct match:
$string = 'fields[name_1]';
preg_match('/(.+)\[(.+)\]/', $string, $matches);
print_r($matches);
You get:
Array
(
[0] => fields[name_1]
[1] => fields
[2] => name_1
)
So, $matches[1] and $matches[2] are what you needed.
Still I am unclear about your exact need!
Here are the explanation for the Regex:
https://regex101.com/r/PcJzQL/3
http://www.phpliveregex.com/
https://www.functions-online.com/preg_match.html
THere are uncounted examples for this alone here on SO. A simple search would have shown you what you need. Anyway, to get you going:
<?php
$subject = 'fields[name_1]';
preg_match('/^(.+)\[(.+)]$/', $subject, $tokens);
print_r($tokens);
The output of that obviously is:
Array
(
[0] => fields[name_1]
[1] => fields
[2] => name_1
)
Related
This question already has answers here:
Split string on spaces except words in quotes
(4 answers)
Closed 3 years ago.
I'm building a website using PHP.
I am using a preg_split() to separate a given string which looks like +word1 -word2 -"word word".
But I need them in the following form +word1, -word2, -"word word".
Currently, I have this one:
$words = preg_split("/[\s\"]*\"([^\"]+)\"[\s\"]*|" . "[\s\"]*'([^']+)'[\s\"]*|" . "[\s\"]+/", $search_expression, 0, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
but it didn't work as I wish: I need to do it in this way to get it work:
+word1 -word2 '-"word word"'.
Does someone have a better regex or idea?
One option is to match from a double quote till a double quote and don't split on that match using SKIP FAIL. Then match 1+ horizontal whitespace chars to split on.
"[^"]+"(*SKIP)(*FAIL)|\h+
Regex demo | Php demo
For example
$search_expression = '+word1 -word2 -"word word"';
$words = preg_split("~\"[^\"]+\"(*SKIP)(*FAIL)|\h+~", $search_expression);
print_r($words);
Output
Array
(
[0] => +word1
[1] => -word2
[2] => -"word word"
)
A simpler expression with greedy ? works for matching your examples:
preg_match_all('/[+-][^+-]+ ?/', $search_expression, $matches);
print_r($matches[0]);
Yields:
Array
(
[0] => +word1
[1] => -word2
[2] => -"word word"
)
Se Example.
This question already has answers here:
What do 'lazy' and 'greedy' mean in the context of regular expressions?
(13 answers)
Closed 8 years ago.
i'm trying to parse tags from a string like the following:
$string = "foo [cmd:tag1] bar [cmd:tag2] bla bla";
$pattern = "/\[cmd:(.+)\]/";
preg_match_all($pattern, $string, $matches, PREG_OFFSET_CAPTURE);
$rc = $matches[0];
foreach($rc as $tag)
{
print_r2($tag);
}
which will return:
Array
(
[0] => [cmd:tag1] bar [cmd:tag2]
[1] => 4
)
what is wrong in my syntax as i'm expecting the following result:
Array
(
[0] => [cmd:tag1]
[1] => [cmd:tag2]
)
thanks
\[cmd:(.+?)\]
or use
\[cmd:([^\]]*)\]
Make your quantifier * non greedy by putting ? ahead of it.
See demo.
https://regex101.com/r/fA6wE2/23
This question already has answers here:
Extract words from string with preg_match_all
(7 answers)
Closed 8 years ago.
I need to extract the names from the following string:
$contact = "John96783819Dickson97863424"
i tried using this:
preg_match('/[a-zA-Z]/',$contact,$matches);
but i get an array with all the alphabets individually in the array.
Desired Output:
Array ([0] => 'John', [2] => 'Dickson')
And now it gets complicated. The same reggae should extract this
$contact = 'Vincent Tan96123179Lawrence Thoo90603123Ryan Ong91235721'
into this
Array ([0] => 'Vincent Tan', [2] => 'Lawrance Thoo' , [3] => 'Ryan Ong')
How do i do that?
All you need is to quantify the character class using +
/[a-zA-Z]+/
+ matches one occurence of presceding regex
Example : http://regex101.com/r/bI6aH1/1
preg_match_all('/[a-zA-Z]+/',$contact,$matches);
Will give output as
Array ( [0] => John [1] => Dickson )
preg_match('/[a-zA-Z]+/',$contact,$matches);
The /[a-zA-Z]/ means match any ONE letter, anywhere in the string. Adding the + in /[a-zA-Z]+/ means match one or MORE sequential letters.
This question already has answers here:
Magnet links library for PHP
(3 answers)
Closed 8 years ago.
I need two items in a magnet link:
magnet:?xt=urn:btih:0eb69459a28b08400c5f05bad3e63235b9853021&dn=Splinter.Cell.Blacklist-RELOADED&tr=udp%3A%2F%2Ftracker.com%3A80&tr=udp%3A%2F%2Ftracker.publicbt.com%3A80&tr=udp%3A%2F%2Ftracker.istole.it%3A6969&tr=udp%3A%2F%2Ftracker.ccc.de%3A80&tr=udp%3A%2F%2Fopen.demonii.com%3A1337
the value of btih:
0eb69459a28b08400c5f05bad3e63235b9853021
and the value of the first udp:
udp://tracker.com:80
How to do this with PHP?
As parse_url() wont help in this situation your have to use regex to parse the string, and then further manipulate the string to get the trackers. So something like:
<?php
$string = 'magnet:?xt=urn:btih:0eb69459a28b08400c5f05bad3e63235b9853021&dn=Splinter.Cell.Blacklist-RELOADED&tr=udp%3A%2F%2Ftracker.com%3A80&tr=udp%3A%2F%2Ftracker.publicbt.com%3A80&tr=udp%3A%2F%2Ftracker.istole.it%3A6969&tr=udp%3A%2F%2Ftracker.ccc.de%3A80&tr=udp%3A%2F%2Fopen.demonii.com%3A1337';
preg_match('#magnet:\?xt=urn:btih:(?<hash>.*?)&dn=(?<filename>.*?)&tr=(?<trackers>.*?)$#', $string, $magnet_link);
//0eb69459a28b08400c5f05bad3e63235b9853021
echo $magnet_link['hash'];
//Splinter.Cell.Blacklist-RELOADED
echo $magnet_link['filename'];
/*[trackers] => Array
(
[0] => udp://tracker.com:80
[1] => udp://tracker.publicbt.com:80
[2] => udp://tracker.istole.it:6969
[3] => udp://tracker.ccc.de:80
[4] => udp://open.demonii.com:1337
)
*/
$magnet_link['trackers'] = explode('&', urldecode(str_replace('tr=','', $magnet_link['trackers'])));
//so to get first tracker
$magnet_link['trackers'][0];
?>
As recommended in the question comments, there appear to be several existing libraries for magnet links - you should probably take a look at these.
If you want to do it yourself however, one way would be to regex for the values. Let's assume that your magnet link is assigned to a variable $link like so:
$link ='magnet:?xt=urn:btih:0eb69459a28b08400c5f05bad3e63235b9853021&dn=Splinter.Cell.Blacklist-RELOADED&tr=udp%3A%2F%2Ftracker.com%3A80&tr=udp%3A%2F%2Ftracker.publicbt.com%3A80&tr=udp%3A%2F%2Ftracker.istole.it%3A6969&tr=udp%3A%2F%2Ftracker.ccc.de%3A80&tr=udp%3A%2F%2Fopen.demonii.com%3A1337';
A quick way to get each value is to run a separate preg_match() for each value - you could combine the two regexes and run preg_match_all() but let's keep it basic. We're going to use lookbehind assertions to try and find the required values.
// your magnet link
$link = 'magnet:?xt=urn:btih:0eb69459a28b08400c5f05bad3e63235b9853021&dn=Splinter.Cell.Blacklist-RELOADED&tr=udp%3A%2F%2Ftracker.com%3A80&tr=udp%3A%2F%2Ftracker.publicbt.com%3A80&tr=udp%3A%2F%2Ftracker.istole.it%3A6969&tr=udp%3A%2F%2Ftracker.ccc.de%3A80&tr=udp%3A%2F%2Fopen.demonii.com%3A1337';
// urls are encoded, let's reverse that
$link = urldecode($link);
// first regex searches for 'btih:' and matches subsequent
// word characters ([a-zA-Z0-9_])
// match(es) are captured as an array to $matchBtih
preg_match('/(?<=btih:)\w+/', $link, $matchBtih);
// same again, more or less, capturing word characters, colon and full-stop
// match(es) are captured as an array to $matchUdp
preg_match('/(?<=tr=)udp:\/\/[\w\:\.]+/', $link, $matchUdp);
// show results
var_dump($matchBtih, $matchUdp);
Should yield:
array (size=1)
0 => string '0eb69459a28b08400c5f05bad3e63235b9853021' (length=40)
array (size=1)
0 => string 'udp://tracker.com:80' (length=20)
Hope this helps :)
This question already has answers here:
Matches text inside brackets with Regex in PHP
(5 answers)
Closed 10 years ago.
i'm sorry i've already asked for this but couldn't find a solution yet :(
here's my string: (as you can see it has linebreaks)
Webname: [webname]
Username: [username]
IP: [IP]
i need to read out the values inside the square brackets.
here's my code:
$pattern = '/\[(.|\n)+?\]/'; // i've used the same syntax for my asp projects, always worked
preg_match($pattern, $txt, $matches, PREG_OFFSET_CAPTURE);
echo "matches:".count($matches)."\n\n";
foreach ($matches as $match)
{
echo $match[0]."\n";
}
i'm getting only 2 matches: [webname] and e (???)
i'm fiddling with this for hours now and can't find out what's wrong ..
any ideas?
thanks
Looks more complicated than it has to be. Line breaks don't play any role here.
$pattern = '/\[(.+?)\]/';
preg_match_all($pattern, $txt, $matches);
print_r($matches);
gives
Array
(
[0] => Array
(
[0] => [webname]
[1] => [username]
[2] => [IP]
)
[1] => Array
(
[0] => webname
[1] => username
[2] => IP
)
)
So the values would be in $matches[1]. If you want the values including the brackets ($matches[0]), you can also omit the parenthesis in the pattern.
The first capture group, in your case there is only one, is in $matches[1]
Try something like
$pattern = '/\[([^\]]+)\]/';
and use preg_match_all() to get all matches.
Try this
$values = array();
foreach (explode("\n", $text) as $line) {
if (preg_match('/([^:]++):[^\s]*+(.*+)/', $line, $match)) {
$values[$match[1]] = $match[2];
}
}
var_dump($values);