need preg_match_all links - php

i have a string like this one:
$string = "some text
http://dvz.local/index/index/regionId/28
http://stuff.kiev.ua/roadmap_page.php http://192.168.3.192/roadmap_page.php
http://192.168.3.192/roadmap_page.php#qwe";
need to get all links.
i tried this way: /http:\/\/(.*)[|\s]?/
returns:
array(2) {
[0] =>
array(3) {
[0] =>
string(42) "http://dvz.local/index/index/regionId/28\r\n"
[1] =>
string(77) "http://stuff.kiev.ua/roadmap_page.php http://192.168.3.192/roadmap_page.php\r\n"
[2] =>
string(41) "http://192.168.3.192/roadmap_page.php#qwe"
}
[1] =>
array(3) {
[0] =>
string(34) "dvz.local/index/index/regionId/28\r"
[1] =>
string(69) "stuff.kiev.ua/roadmap_page.php http://192.168.3.192/roadmap_page.php\r"
[2] =>
string(34) "192.168.3.192/roadmap_page.php#qwe"
}
}
EDIT 1:
expect:
array(2) {
[0] =>
array(3) {
[0] =>
string(42) "http://dvz.local/index/index/regionId/28"
[1] =>
string(77) "http://stuff.kiev.ua/roadmap_page.php"
[2] =>
string(77) "http://192.168.3.192/roadmap_page.php"
[3] =>
string(41) "http://192.168.3.192/roadmap_page.php#qwe"
}
[1] =>
array(3) {
[0] =>
string(34) "dvz.local/index/index/regionId/28"
[1] =>
string(69) "stuff.kiev.ua/roadmap_page.php"
[2] =>
string(69) "192.168.3.192/roadmap_page.php"
[3] =>
string(34) "192.168.3.192/roadmap_page.php#qwe"
}
}

Try this one:
/http:\/\/([^\s]+)/

Try this:
preg_match_all('|http://([^\s]*)|', $string, $matches);
var_dump($matches);

All links from text
http[s]?[^\s]*

Numerous pages have only relative links to the main document, (thus no http(s):// ... to parse), for those the following works fine, splitting by the href attribute:
preg_match_all('|href="([^\s]*)"><\/a>|', $html, $output_array);
Or even simpler:
preg_match_all('|href="(.*?)"><\/a>|', $html, $output_array);
Example output:
[0]=>
string(56) "/broadcast/bla/xZr300"
[1]=>
string(50) "/broadcast/lol/fMoott"

Related

Split lines of space-delimited values to get flat array of 2nd and 3rd column values

I have the following data which is displaying as this
{123456 123456 123456}
{654321 654321 654321}
{123456 123456 123456}
My PHP Code:
$myarray = preg_split("/(\s|{\s)/", $data);
print_r($myarray);
The output of my array is like this:
[0] => {123456
[1] => 123456
[2] => 123456}
[3] => {654321
[4] => 654321
[5] => 654321}
[6] => {123456
[7] => 123456
[8] => 123456}
My question is, how to hide [0], [3] and [6] from the output? if you noticed, they start with a {
I'm not sure if I did a mistake coding the preg_split function
Desired behavior:
if the data is like this
{1 2 3}
{4 5 6}
{7 8 9}
the desired output should be like this:
[0] => 2
[1] => 3
[2] => 5
[3] => 6
[4] => 8
[5] => 9
Not everything needs a regular expression.
$input = <<<_E_
{123456 123456 123456}
{654321 654321 654321}
{123456 123456 123456}
_E_;
$lines = explode("\n", $input);
$lines = array_map(function($a){return trim($a, '{}');}, $lines);
$lines = array_map(function($a){return explode(' ', $a);}, $lines);
$lines = array_map('array_filter', $lines);
$items = array_merge(...$lines);
var_dump($lines, $items);
Output:
array(3) {
[0]=>
array(3) {
[0]=>
string(6) "123456"
[2]=>
string(6) "123456"
[4]=>
string(6) "123456"
}
[1]=>
array(3) {
[0]=>
string(6) "654321"
[2]=>
string(6) "654321"
[4]=>
string(6) "654321"
}
[2]=>
array(3) {
[0]=>
string(6) "123456"
[2]=>
string(6) "123456"
[4]=>
string(6) "123456"
}
}
array(9) {
[0]=>
string(6) "123456"
[1]=>
string(6) "123456"
[2]=>
string(6) "123456"
[3]=>
string(6) "654321"
[4]=>
string(6) "654321"
[5]=>
string(6) "654321"
[6]=>
string(6) "123456"
[7]=>
string(6) "123456"
[8]=>
string(6) "123456"
}
Change {\s to {\d+\s so that {123456 will be a delimiter and not be included in the result.
You don't need the capture group around the regular expression.
$data = '{123456 123456 123456}
{654321 654321 654321}
{123456 123456 123456}';
$myarray = preg_split("/\s|{\d+\s/", $data);
print_r($myarray);
Output:
Array
(
[0] =>
[1] =>
[2] => 123456
[3] =>
[4] => 123456}
[5] =>
[6] =>
[7] => 654321
[8] =>
[9] => 654321}
[10] =>
[11] =>
[12] => 123456
[13] =>
[14] => 123456}
)
If you also don't want } in the results, that needs to be in the regexp as well.
$myarray = preg_split("/\s+|\s*\{\d+\s*|\s*\}\s*/", $data);
You could also use a regular expression that matches a number unless it's preceded by {, using a negative lookbehind.
$data = '{1 2 3} {4 5 6} {7 8 9}';
preg_match_all('/(?<!{)\d+/', $data, $match);
$myarray = $match[0];
print_r($myarray);
Output:
Array
(
[0] => 2
[1] => 3
[2] => 5
[3] => 6
[4] => 8
[5] => 9
)
Not everything needs a regular expression, but sometimes it is the most direct tool for the job.
Split the string on all characters that you don't want to keep.
Code: (Demo)
var_export(
preg_split(
'/ +|}\R?|{\d+/',
$text,
0,
PREG_SPLIT_NO_EMPTY
)
);
Not sure if preg_split() is required, but you could do this with a match all and then gather what's found:
$foo = trim('
{555555 123456 123456}
{666666 654321 654321}
{777777 123456 123456}
');
$items = [];
if (preg_match_all('/^{[\d]{1,} ([\d]{1,}) ([\d]{1,})}$/m', $foo, $matches)) {
$items = array_reduce(
array_slice($matches, 1),
fn(array $found, array $match) => array_merge($found, $match),
[]
);
}
var_dump($items);
Gives:
array(6) {
[0]=>
string(6) "123456"
[1]=>
string(6) "654321"
[2]=>
string(6) "123456"
[3]=>
string(6) "123456"
[4]=>
string(6) "654321"
[5]=>
string(6) "123456"
}
https://3v4l.org/n9tRk

Compilation failed: POSIX collating elements are not supported at offset

i would like transform a string to array with pattern. But my regex give me the warning.
this is a string:
$string = typ="bar" title="Example" enabled=true count=true style="float: left; width: 30%;"
My regex:
$regex='/(.*?)[=\"|=](.*?)\"*\s*/';
preg_match_all($regex, $string1, $matchesreg, PREG_SET_ORDER);
Whith the regex is the output not correct. The last array must be split further
$regex='/(.?)="(.?)"\s*/';
preg_match_all($regex, $string1, $matchesreg, PREG_SET_ORDER);
The output
Array
(
[0] => Array
(
[0] => typ="bar"
[1] => typ
[2] => bar
) ...
[2] => Array
(
[0] => enabled=true count=true style="float: left; width: 30%;"
[1] => enabled=true count=true style
[2] => float: left; width: 30%;
)
)
My desired output like:
php
Array
(
[0] => Array
(
[0] => typ="bar"
[1] => typ
[2] => bar
)
[1] => Array
(
[0] => title="Example"
[1] => title
[2] => Example
)
[2] => Array
(
[0] => enabled=true
[1] => enabled
[2] => true
)
[3] => Array
(
[0] => count=true
[1] => count
[2] => true
)
[4] => Array
(
[0] => style="float: left; width: 30%;"
[1] => style
[2] => float: left; width: 30%;
)
)
You may use
preg_match_all('~([^\s=]+)=(?|"([^"]*)"|(\S+))~', $s, $m, PREG_SET_ORDER, 0)
See the PHP demo
Details
([^\s=]+) - Group 1: one or more chars other than whitespace and =
= - a = char
(?|"([^"]*)"|(\S+)) - a branch reset group matching either of
"([^"]*)" - ", then any 0 or more chars other than " are captured into Group 2, and then " is matched
| - or
(\S+) - Group 2: one or more non-whitespace chars.
Your expression seems to be working fine. There were two = in your char list that I removed one of them:
(.*?)[=\"|](.*?)\"*\s*
You can modify/change your expressions in this link, if you wish.
RegEx Circuit
You can visualize your expressions in this link:
Code
$string1 = 'typ="bar" title="Example" enabled=true count=true style="float: left; width: 30%;';
$regex = '/(.*?)[=\"|](.*?)\"*\s*/s';
preg_match_all($regex, $string1, $matchesreg, PREG_SET_ORDER);
var_dump($matchesreg);
Output
array(7) {
[0]=>
array(3) {
[0]=>
string(5) "typ=""
[1]=>
string(3) "typ"
[2]=>
string(0) ""
}
[1]=>
array(3) {
[0]=>
string(5) "bar" "
[1]=>
string(3) "bar"
[2]=>
string(0) ""
}
[2]=>
array(3) {
[0]=>
string(7) "title=""
[1]=>
string(5) "title"
[2]=>
string(0) ""
}
[3]=>
array(3) {
[0]=>
string(9) "Example" "
[1]=>
string(7) "Example"
[2]=>
string(0) ""
}
[4]=>
array(3) {
[0]=>
string(8) "enabled="
[1]=>
string(7) "enabled"
[2]=>
string(0) ""
}
[5]=>
array(3) {
[0]=>
string(11) "true count="
[1]=>
string(10) "true count"
[2]=>
string(0) ""
}
[6]=>
array(3) {
[0]=>
string(12) "true style=""
[1]=>
string(10) "true style"
[2]=>
string(0) ""
}
}

extract domain subdomain from spf record with php regex pattern

I want to extract the subdomain & domain name from an spf record using the regex php.
$spfreccord= ' v=spf1 include:smtproutes.com include:smtpout.com ip4:46.163.100.196 ip4:46.163.100.194 ip4:85.13.135.76 ip4:178.255.156.110 ip4:188.172.204.21 ip4:178.255.154.52 ip4:188.172.233.6 ip4:37.252.230.29 ip4:217.146.22.37 ~all';
spfreccord2="v=spf1 include:_netblocks.google.com include:_netblocks2.google.com include:_netblocks3.google.com ~all";
$regexdomain = '/\s*([-a-z0-9]+\.)+' . implode("|", $extensionList) . '\s*/i';
preg_match_all($regexdomain, $spfreccord,$matchesdom);
// echo the result
foreach ($matchesdom as $ke) {
foreach ($ke as $domspf) {
echo $domspf;
echo "<br>";
}
}
what kind of regex should i use to extract simple domain & subdomain like _netblocks.google.com
I couldn't clearly get what you are asking. But here what I think you need:
$spfreccord ='v=spf1 include:smtproutes.com include:smtpout.com ip4:46.163.100.196 ip4:46.163.100.194 ip4:85.13.135.76 ip4:178.255.156.110 ip4:188.172.204.21 ip4:178.255.154.52 ip4:188.172.233.6 ip4:37.252.230.29 ip4:217.146.22.37 ~all';
$spfreccord2 ="v=spf1 include:_netblocks.google.com include:_netblocks2.google.com include:_netblocks3.google.com ~all";
$domainExts = ["com", "net", "org", "io"]; // fill according to your needs
$regex = "/:([\w]*?)\\.?([\w]*?)\\.(".implode("|", $domainExts).")/";
preg_match_all($regex, $spfreccord2, $output);
// $output[1] => Subdomains.
// $output[2] => Domains
// $output[3] => Domain extension
var_dump($output);
/*
array(4) {
[0] =>
array(3) {
[0] =>
string(22) ":_netblocks.google.com"
[1] =>
string(23) ":_netblocks2.google.com"
[2] =>
string(23) ":_netblocks3.google.com"
}
[1] =>
array(3) {
[0] =>
string(10) "_netblocks"
[1] =>
string(11) "_netblocks2"
[2] =>
string(11) "_netblocks3"
}
[2] =>
array(3) {
[0] =>
string(6) "google"
[1] =>
string(6) "google"
[2] =>
string(6) "google"
}
[3] =>
array(3) {
[0] =>
string(3) "com"
[1] =>
string(3) "com"
[2] =>
string(3) "com"
}
}
*/
preg_match_all('/(ftp|http|https):\/\/(.*?)[\.][a-zA-Z]{1,3}(.*?)/',$str_search,$arr,PREG_PATTERN_ORDER);
print_r($arr);
Domain:$arr[0][0];
Subdomain:$arr[2][0];

Exclude data from brackets using regex (preg_match_all)

Input string:
:txt{sometext}:alpha
I want to extract data like this (extracted from brackets):
Result using preg_match_all():
sometext
Trying like this, but none of this works:
php > preg_match_all('/^(\:txt)(.*)+(\{)(.*)+(\})/i', ':txt{sometext}:alpha', $m); var_dump($m);
array(6) {
[0] =>
array(1) {
[0] =>
string(14) ":txt{sometext}"
}
[1] =>
array(1) {
[0] =>
string(1) ":"
}
[2] =>
array(1) {
[0] =>
string(0) ""
}
[3] =>
array(1) {
[0] =>
string(1) "{"
}
[4] =>
array(1) {
[0] =>
string(0) ""
}
[5] =>
array(1) {
[0] =>
string(1) "}"
}
}
Note: as sample I have like this :txt{sometext}:alpha:another{mydata}, so I can extract data from :another and give results like mydata.
RESULTS:
Result from Sniffer:
php > preg_match_all('/(?<=:txt{)([^}]+)(?=})/', ':txt{sometext}:alpha', $x); var_dump($x);
array(2) {
[0] =>
array(1) {
[0] =>
string(8) "sometext"
}
[1] =>
array(1) {
[0] =>
string(8) "sometext"
}
}
Result from Jerry:
php > preg_match_all('/^:txt\{([^}]+)\}/', ':txt{sometext}:alpha', $x); var_dump($x);
array(2) {
[0] =>
array(1) {
[0] =>
string(14) ":txt{sometext}"
}
[1] =>
array(1) {
[0] =>
string(8) "sometext"
}
}
Why all this, why not just:
(?<=:txt{)([^}]+)(?=})
Regex101 Demo

Display array elements in smarty

I have merged two mysql results :
while($rs_1 = mysql_fetch_array($r1))
{
$arr1[] = $rs_1;
}
while($rs_2 = mysql_fetch_array($r2))
{
$arr2[] = $rs_2;
}
$resN = array_merge($arr1,$arr2);
var_dump($resN) shows the following result :
array(5) {
[0] => array(4) {
[0] => string(6) "Petric"
["bz_pro_first_name"] => string(6) "Petric"
[1] => string(8) "Naughton"
["bz_pro_last_name"] => string(8) "Naughton"
}
[1] => array(4) {
[0] => string(6) "Nitish"
["bz_pro_first_name"] => string(6) "Nitish"
[1] => string(12) "Dolakasharia"
["bz_pro_last_name"] => string(12) "Dolakasharia"
}
[2] => array(4) {
[0] => string(6) "Martin"
["bz_pro_first_name"] => string(6) "Martin"
[1] => string(3) "Rom"
["bz_pro_last_name"] => string(3) "Rom"
}
[3] => array(4) {
[0] => string(5) "Steve"
["bz_pro_first_name"] => string(5) "Steve"
[1] => string(5) "Wough"
["bz_pro_last_name"] => string(5) "Wough"
}
[4] => array(4) {
[0] => string(3) "Liz"
["bz_pro_first_name"] => string(3) "Liz"
[1] => string(6) "Hurley"
["bz_pro_last_name"] => string(6) "Hurley"
}
}
I am supposed to display them in smarty so :
assign_values('rand_ad',$resN);
Now I tried to display in smarty like this :
{foreach name=outer item=pro from=$rand_pro}
{foreach key=key item=item from=$pro}
{$key}: {$item}<br />
{/foreach}
{/foreach}
It displays the results but serially. I need to extract the values in some positions . So how can I extract the values eg first name, last name etc ?
You didn't give an example output of what you wanted, so I'll take a guess. Assuming you are looking for the following output for the array above:
0: Petric Naughton<br />
1: Nitish Dolakasharia<br />
2: Martin Rom<br />
3: Steve Wough<br />
4: Liz Hurley<br />
You'd could do this:
{foreach from=$rand_pro item=pro key=pro_key}
{$pro_key}: {$pro.bz_pro_first_name} {$pro.bz_pro_last_name}<br />
{/foreach}
If I am assuming incorrectly, please update your question with the output you are expecting to be more clear.
Use {foreach} to display array in smarty.
and for database connection use ADODB.
In php file:
type $smarty->assign->("arrname",$aodDBconn->Execute($sql)).
It will work.

Categories