Get only internal links and external from array (RegEx and PHP) - php

This is my array output:
Array
(
[0] => http://www.example.com/some-page/
[1] => http://www.example.com/another-page/
[2] => http://www.example.com/third-page/
[3] => https://www.example.com/ssl/
[4] => https://www.example.com/with-slash-at-the-end/
[5] => https://www.example.com/without-slash-at-the-end
[6] => /internal-link
[7] => /anther-internal-link/
[8] => https://www.my-own-domain.com/internal-link-too-but-with-absolute-path/
)
How can I get only external and only internal links from this array? I doesn't matter which one.

This regex seems to work fine for finding internal link
(.*my-own-domain\.com.*)|(^\/.*$)
Regex Demo
PHP Code
$re = "/(?:.*my-own-domain\\.com.*)|(?:^\\/.*$)/m";
foreach($str as $x) {
if (preg_match($re, $x)) {
echo $x . "" . "\n";
}
}
Ideone Demo

Related

How to extract certain words from a php string?

I have a long string like this I1:1;I2:2;I8:2;NA1:5;IA1:[1,2,3,4,5];S1:asadada;SA1:[1,2,3,4,5];SA1:[1,2,3,4,5];. Now I just want to get certain words like 'I1','I2','I8','NA1' and so on i.e. words between ':'&';' only ,and store them in array. How to do that efficiently?
I have already tried using preg_split() and it works but giving me wrong output. As shown below.
// $a is the string I want to extract words from
$str = preg_split("/[;:]/", $a);
print_r($str);
The output I am getting is this
Array
(
[0] => I8
[1] => 2
[2] => I1
[3] => 1
[4] => I2
[5] => 2
[6] => I3
[7] => 2
[8] => I4
[9] => 4
[10] =>
)
Array
(
[0] => NA1
[1] => 5
[2] =>
)
Array
(
[0] => IA1
[1] => [1,2,3,4,5]
[2] =>
)
Array
(
[0] => S1
[1] => asadada
[2] =>
)
Array
(
[0] => SA1
[1] => [1,2,3,4,5]
[2] =>
)
But I am expecting 'I8','I1','I2','I3','I4' also in seperated array with position [0]. Any help on how to do this.
You could try something like.
<?php
$str = 'I1:1;I2:2;I8:2;NA1:5;IA1:[1,2,3,4,5];S1:asadada;SA1:[1,2,3,4,5];SA1:[1,2,3,4,5];';
preg_match_all('/(?:^|[;:])(\w+)/', $str, $result);
print_r($result[1]); // Matches are here in $result[1]
You can perform a greedy match to match the items between ; and : using preg_match_all()
<?php
$str = 'I1:1;I2:2;I8:2;NA1:5;IA1:[1,2,3,4,5];S1:asadada;SA1:[1,2,3,4,5];SA1:[1,2,3,4,5];';
preg_match_all('/;(.+?)\:/',$str,$matches);
print_r($matches[1]);
Live Demo: https://3v4l.org/eBsod
One possible approach is using a combination of explode() and implode(). The result is returned as a string, but you can easily put it into an array for example.
<?php
$input = "I1:1;I2:2;I8:2;NA1:5;IA1:[1,2,3,4,5];S1:asadada;SA1:[1,2,3,4,5];SA1:[1,2,3,4,5];.";
$output = array();
$array = explode(";", $input);
foreach($array as $item) {
$output[] = explode(":", $item)[0];
}
echo implode(",", $output);
?>
Output:
I1,I2,I8,NA1,IA1,S1,SA1,SA1,.

PHP - strpos on array elements?

For some reason I can't get strpos to work to search my array, even if $jobList[1] and $titlesearch are the same values... Sorry if it's something obvious but I'm still pretty new to coding!
I begin with my $data array which looks like this:
Array
(
[0] => P0001 Lifeguard descexample 18/09/18 parttime fixedterm mail vic
[2] => P0002 IT Manager descexample 18/09/18 fulltime ongoing post mail sa
)
I then explode each of these entries into their own array...
for ($i = 0; $i < count($data); $i++) {
$jobList = explode("\t", $data[$i]);
}
Array
(
[0] => P0001
[1] => Lifeguard
[2] => descexample
[3] => 18/09/18
[4] => parttime
[5] => fixedterm
[6] =>
[7] => mail
[8] => vic
)
Array
(
[0] => P0002
[1] => IT Manager
[2] => descexample
[3] => 18/09/18
[4] => fulltime
[5] => ongoing
[6] => post
[7] => mail
[8] => sa
)
Now I'm trying to search through these arrays from a user input, $titlesearch, and find it's matches with the job titles, $jobList[1]:
if (strpos($jobList[1], $titlesearch)) {
echo "nice one";
}
No matter what loops I try, the strpos never returns true, even if I echo the values and they both give the same result, so I'm really not sure what I'm doing wrong :'(
Any help is greatly appreciated!
You should always compare the data type when using this function as it may not return a boolean and it can be missleading. Check documentation here
Try it something like this:
if (strpos($jobList[1], $titlesearch) !== false) {
echo "nice one";
}

php returned array from preg_match_all

i have an array that is being returned like this:
Array ( [0] => Array ( [0] => ;3750;011; [1] => ;3750;012; [2] => ;3750;013; [3] => ;3750;014; [4] => ;3750;015; [5] => ;3750;016; [6] => ;3750;017; [7] => ;3750;018; [8] => ;3750;019; ))
the array is coming from preg_match_all
I have tried to print it with foreach loop and it always returns the same way
i can't work with it like this.. and i do not understand what is going on
this is the preg_match_all that it comes from:
$remove = preg_match_all('/;([\d]{4};[\d]{3});/', $str, $m);
preg_match_all() returns in match result an array of arrays. Then to display all the whole matches you must use:
$remove = preg_match_all('/;([\d]{4};[\d]{3});/', $str, $m);
foreach($m[0] as $item) { echo $item . '<br/>'; }
If you only want the content of your capturing group, just replace $m[0] by $m[1]

PHP preg_grep not working?

$fileNameMatchRegex = ^a-[0-9]*_b-[0-9]*_c-.*(_d-on)?_((19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01]))-[0-9]*\.csv$
$fileNames =
Array
(
[0] => index.html
[1] => a-34234234_b-3271_c-123_d-on_2013-08-12-10.csv
[2] => a-52342345_b-3271_c-123_d-on_2013-08-12-11.csv
[3] => a-8764453_b-3271_c-123_d-on_2013-08-12-12.csv
[4] => a-7654334_b-3271_c-1234_d-on_2013-08-12-4.csv
[5] => a-3435_b-3271_c-23re_d-on_2013-08-12-5.csv
[6] => a-909876_b-3271_c-wef2r2_d-on_2013-08-12-6.csv
[7] => a-345456_b-3271_c-23rwef_d-on_2013-08-12-7.csv
[8] => a-98765_b-3271_c-23ref_d-on_2013-08-12-8.csv
[9] => a-098765_b-3271_c-wef2r_d-on_2013-08-12-9.csv
)
$matchingFileNames = preg_grep ("/".$fileNameMatchRegex."/", $fileNames);
This works here...I get a match on 1-9: regex tester
Its working fine for me
$fileNames = explode(',', "index.html,a-34234234_b-3271_c-123_d-on_2013-08-12-10.csv,a-52342345_b-3271_c-123_d-on_2013-08-12-11.csv,a-8764453_b-3271_c-123_d-on_2013-08-12-12.csv,a-7654334_b-3271_c-1234_d-on_2013-08-12-4.csv,a-3435_b-3271_c-23re_d-on_2013-08-12-5.csv,a-909876_b-3271_c-wef2r2_d-on_2013-08-12-6.csv,a-345456_b-3271_c-23rwef_d-on_2013-08-12-7.csv,a-98765_b-3271_c-23ref_d-on_2013-08-12-8.csv,a-098765_b-3271_c-wef2r_d-on_2013-08-12-9.csv");
$fileNameMatchRegex = '^a-[0-9]*_b-[0-9]*_c-.*(_d-on)?_((19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01]))-[0-9]*\.csv$';
$matchingFileNames = preg_grep ("#".$fileNameMatchRegex."#", $fileNames);
print_r($fileNames);
print_r($matchingFileNames);
but i used '#' around the regexp as delimiter, as your regexp included '/',
you proberly just need to escape your '/' or chose a better delimiter

preg_match or operator

My code below produces an error, unknown modified "|"... I'm trying to use it as the OR operator. What is the correct way to run this code without error?
$p = "(\w+)|(\()|(\))|(\,)";
$s = "sum(blue,paper,green(yellow,4,toast)))";
preg_match($p,$s, $matches);
print_r($matches);
Edit
Okay I changed it a bit... ~(\w+|\(|\)|,)~
Now... here's the problem: I need to take that string and split it into an array like this:
array("sum","(","blue","paper","green","(" ... etc );
Can someone help me do that? when I run the above expression it outputs an empty array....
Thanks
You are missing the delimiter for your pattern.
$p = "~(\w+)|(\()|(\))|(\,)~";
You're missing the delimiter as #Crayon correctly mentioned, also this pattern does the same thing:
$p = '~(\w+|[(]|[)]|,)~';
As for your (new) problem, try this:
$p = '~([(),])~';
$str = 'sum(blue,paper,green(yellow,4,toast)))';
$res = preg_split($p, $str, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
print_r($res);
Output:
Array
(
[0] => sum
[1] => (
[2] => blue
[3] => ,
[4] => paper
[5] => ,
[6] => green
[7] => (
[8] => yellow
[9] => ,
[10] => 4
[11] => ,
[12] => toast
[13] => )
[14] => )
[15] => )
)

Categories