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 :)
Related
I am trying to create an array from input in a textarea.
I have a file named with a textarea.The textarea might contain values like(exactly the way it looks):
CREATE this.
DO that.
STOP it.
Basically,I want to use PHP to :
Create an array out of the values given by the textarea for example,the array madefrom th values of the textarea above is meant to be :
Array
(
[0] => create this
[1] => do this
[2] => Stop it
)
I'v e tried the following code:
<?php
$wholecode=$_POST['code'];
$code=explode('.',trim(strtolower($wholecode)));//convert code to array
$words=explode(' ', $code);
print_r($code);
I get
Array
(
[0] => create this
[1] =>
do this
[2] =>
stop it
[3] =>
)
As it clearly shows,that is not what I want.Please help
You just need to tidy up the array contents after creating it. You have things like new lines and potentially other whitespace around the content.
This uses array_map() to trim() each entry in the array. Then uses array_filter() to remove any empty elements (calling it with no callback will do this).
$wholecode=$_POST['code'];
$code=explode('.',trim(strtolower($wholecode)));//convert code to array
$code=array_map("trim", $code );
$code = array_filter($code);
print_r($code);
Here's a direct approach using a simple regex pattern to explode on dots followed by zero or more whitespace characters. No mopping up after splitting.
Code (Demo)
$_POST['code'] = 'CREATE this.
DO that.
STOP it.';
var_export(preg_split('~\.\s*~', strtolower($_POST['code']), -1, PREG_SPLIT_NO_EMPTY));
Output:
array (
0 => 'create this',
1 => 'do that',
2 => 'stop it',
)
As a variable...
$array = preg_split('~\.\s*~', strtolower($_POST['code']), -1, PREG_SPLIT_NO_EMPTY)
Explanation transferred from my comment:
Pattern Demo: https://regex101.com/r/jygaQ1/1
There are 3 dots in your sample data. The first two have whitespaces characters that immediately follow. The final dot has no trailing whitespace characters.
The \s* means "match zero or more whitespace characters.
-1 means perform unlimited explosions.
PREG_SPLIT_NO_EMPTY means that on the final explosion (the last dot) there will be an empty element generated, but preg_split() will disregard it in the output array.
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
)
I am trying to figure out a way using PHP that I can parse a string of data from a URL to separate each entry based on the character |. Example data:
66.85.14.212:7254|108.174.48.186:8340|72.46.152.194:15240|162.248.91.125:7266|91.121.83.48:7619|185.153.228.114:5775|
Anyone got any ideas?
You can use the explode() function to split the string and remove empty elements with array_filter()
$string = '66.85.14.212:7254|108.174.48.186:8340|72.46.152.194:15240|162.248.91.125:7266|91.121.83.48:7619|185.153.228.114:5775|';
$ips = array_filter(explode('|', $string));
which results in an array containing all your ip adresses
Array
(
[0] => 66.85.14.212:7254
[1] => 108.174.48.186:8340
[2] => 72.46.152.194:15240
[3] => 162.248.91.125:7266
[4] => 91.121.83.48:7619
[5] => 185.153.228.114:5775
)
There are also two other functions, split() and preg_split() which supports regular expressions too.
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:
php explode all characters [duplicate]
(4 answers)
Closed 8 years ago.
how i can explode string into an array. Acctually i want to translate english language into the braille. First thing i need to do is to get the character one by one from a string,then convert them by mathing the char from value in database and display the braille code using the pic. As example when user enter "abc ef", this will create value separately between each other.
Array
(
[0] => a
[1] => b
[2] => c
[3] =>
[4] => e
[5] => f
)
i tried to do but not get the desired output. This code separated entire string.
$papar = preg_split('/[\s]+/', $data);
print_r($papar);
I'm sorry for simple question,and if you all have an idea how i should do to translate it, feel free to help. :)
If you're using PHP5, str_split will do precisely what you're seeking to accomplish. It splits each character in a string – including spaces – into an individual element, then returns an array of those elements:
$array = str_split("abc ef");
print_r($array);
Outputs the following:
Array
(
[0] => a
[1] => b
[2] => c
[3] =>
[4] => e
[5] => f
)
UPDATE:
The PHP4 solution is to use preg_split. Pass a double forward slash as the matching pattern to delimit the split character-by-character. Then, set the PREG_SPLIT_NO_EMPTY flag to ensure that no empty pieces will be returned at the start and end of the string:
$array = preg_split('//', "abc ef", -1, PREG_SPLIT_NO_EMPTY); // Returns same array as above
PHP has a very simple method of getting the character at a specific index.
To do this, utilize the substring method and pass in the index you are looking for.
How I would approach your problem is
Ensure the string you are converting has at least a size of one
Determine how long the string is
int strlen ( string $string )
Loop over each character and do some operation depending on that character
With a string "abcdef"
$rest = substr("abcdef", -1); // returns "f"
See the substr page for a full example and more information
http://us2.php.net/substr
You can iterate the string itself.
$data = 'something string';
for($ctr = 0; $ctr < strlen($data); $ctr++)
{
if($data{$ctr}):
$kk = mysql_query("select braille_teks from braille where id_huruf = '$data{$ctr}'");
$jj = mysql_fetch_array($kk);
$gambar = $jj['braille_teks'];
?>
<img src="images/<?php echo $gambar;?>">
<?php
else:
echo " ";
}