How to use an ordered array in php - php

I want to extract the website name, from a link, so I write the following function:
protected function getWebsiteName()
{
$prefixs = ['https://', 'http://', 'www.'];
foreach($prefixs as $prefix)
{
if(strpos($this->website_link, $prefix) !== false)
{
$len = strlen($prefix);
$this->website_name = substr($this->website_link, $len);
$this->website_name = substr($this->website_name, 0, strpos($this->website_name, '.'));
}
}
}
The problem is that when I use I website link that look like https://www.github.com, the result is: s://www, and the function only works when I remove that 'www.' from the array list.
Any ideas why this is happening, or how I can improve this function?

You could use parse_url();, Try:
print_r(parse_url('https//www.name/'));

Let's look at your code. Each time you go through the foreach, you are applying your logic from the original website_link every time. This means when you run strlen in the situation of www. after the first two iterations, this happens:
$prefix is www.
Therefore, $len = 4 (the length of $prefix)
$this->website_link is still https://www.github.com
You apply substr($this->website_link, 4)
Result is $this->website_name = 's://www.github.com'
You apply substr($this->website_name, 0, 7) (7 being the result of strpos($this->website_name, '.')
The result is $this->website_name = 's://www'
To fix this, you should save $this->website_link to $temp and then use the following code:
$temp = $this->website_link;
foreach($prefixs as $prefix)
{
if(strpos($temp, $prefix) !== false)
{
$len = strlen($prefix);
$temp = substr($temp, $len);
}
}
$this->website_name = substr($temp, 0, strpos($temp, '.'));
I'd suggest #dynamic's answer, but if you want to continue the strategy of string replacement, use str_replace. It accepts arrays for the needle!
$prefixes = ['https://', 'http://', 'www.'];
$this->website_name = str_replace($prefixes, '', $this->website_link);
$this->website_name = substr($this->website_name, 0, strpos($this->website_name, '.'));

Yes, use parse_url along with preg_match should do the job
function getWebsiteName($url)
{
$pieces = parse_url($url);
$domain = isset($pieces['host']) ? $pieces['host'] : '';
if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs)) {
return $regs['domain'];
}
return false;
}
This is fixing your code.
function getWebsiteName()
{
$this->website_name = $this->website_link;
$prefixs = array('https://', 'http://', 'www.');
foreach($prefixs as $prefix)
{
if (substr($this->website_name, 0, strlen($prefix)) == $prefix) {
$this->website_name = substr($this->website_name, strlen($prefix));
}
}
}

Related

How to seperate the string of the variable in PHP from requested word

$t = ABCDEFPN03KSASDEID;
$r = stristr($t, 'PN03', true);
My Value is $r = ABCDEF
and in if condition while i plan to seperate it throws output wrong
But i want output $s = PNO3KSASDEID like this
My code:
(I used variable as my own)
$r = stristr($t, 'PN03', true);
if( ($s = substr($t, 'PNO3', false)) == true) {
$s = stristr($t, 'PNO3', false);
print_r($s);
}
output: PN03KSASDEID is needed.
Please suggest me solution.
Get in the habit of encapsulating your behaviours in reusable functions
function str_split_term($string, $term) {
$pos = strpos($string, $term);
if ($pos === false)
return $string;
else
return substr($string, $pos);
}
Your use case
echo str_split_term('ABCDEFPN03KSASDEID', 'PN03'); // PN03KSASDEID
If the term is not found in the string, the string will output unaltered
echo str_split_term('ABCDEFPN03KSASDEID', 'ZZZ'); // ABCDEFPN03KSASDEID
PHP7
If you're are using a current version of PHP, I suggest you add type hints to the function
function str_split_term(string $string, string $term): string {
$pos = strpos($string, $term);
if ($pos === false)
return $string;
else
return substr($string, $pos);
}

echo partially hidden (IPV4 or IPV6) ip in a table

I have a table where a variable in a row containing IP information is being echoed. I think there's an issue with my if statement, because if I use the following then I can get the variable to echo:
echo $row['log_ip'] = substr_replace ($row['log_ip'], $ipv4replacement, stripos ($row['log_ip'], $ipv4needle, $offset = 2));
My current code:
$ipv6needle = ':';
$ipv4needle = '.';
$ipv4replacement = '.***.***.***';
$ipv6replacement = ':****:****:****:****:****:****:****';
if (strpos($row['log_ip'], ':') !== FALSE) {
echo $row['log_ip'] = substr_replace ($row['log_ip'], $ipv4replacement, stripos ($row['log_ip'], $ipv4needle, $offset = 2));
else
echo $row['log_ip'] = substr_replace ($row['log_ip'], $ipv6replacement, stripos ($row['log_ip'], $ipv6needle, $offset = 2)); }
Your code is full of weird assignments, I don't know what you want to achieve with them, but here's a corrected and refactored code.
<?php
$offset = 2;
if (strpos($row["log_ip"], ":") !== false) {
$needle = ".";
$replacement = ".***.***.***";
}
else {
$needle = ":";
$replacement = ":****:****:****:****:****:****:****";
}
$row["log_ip"] = substr_replace($row["log_ip"], $replacement, stripos($row["log_ip"], $needle, $offset));
echo $row["log_ip"];
Answer to the question in the comment:
<?php
function mask_ip_address($ip_address) {
if (strpos($ip_address, ".") !== false) {
$parts = explode(".", $ip_address);
return $parts[0] . str_repeat(".***", 3);
}
$parts = explode(":", $ip_address);
return $parts[0] . str_repeat(":****", 7);
}
function mask_ip_address_test($ip_address, $expected) {
assert($expected === mask_ip_address($ip_address));
}
mask_ip_address_test("17.0.0.1", "17.***.***.***");
mask_ip_address_test("fe80::200:5aee:feaa:20a2", "fe80:****:****:****:****:****:****:****");

How do I compare two domain names in URL strings?

Say if I have two strings
$first = 'http://www.example.com';
$second = 'www.example.com/';
How could I determine they match? I just care that the example part matches. I'm thinking some form of Regex pattern would match but I can't figure it out at all.
Don't use a regex if you're trying to evaluate structured data. Regexes are not a magic wand you wave at every problem that happens to involve strings. What if you have a URL like http://www.some-other-domain.com/blah/blah/?www.example.com?
If you're trying to match a domain name to a domain name, then break apart the URL to get the host and compare that. In PHP, use the parse_url function. That will give you www.example.com as the host name, and then you can compare that to make sure it is the hostname you expect.
Try this
function DomainUrl($x) {
$url = $x;
if ( substr($url, 0, 7) == 'http://') { $url = substr($url, 7); }
if ( substr($url, 0, 8) == 'https://') { $url = substr($url, 8); }
if ( substr($url, 0, 4) == 'www.') { $url = substr($url, 4); }
if ( substr($url, 0, 4) == 'www9.') { $url = substr($url, 4); }
if ( strpos($url, '/') !== false) {
$ex = explode('/', $url);
$url = $ex['0'];
}
return $url;
}
$first = DomainUrl('http://www.example.com');
$second = DomainUrl('www.example.com/');
if($first == $second){
echo 'Match';
}else{
echo 'Not Match';
}

How can I access a multidimensional array element by path? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP: Use a string as an array index path to retreive a value
I have an array like so:
$array['image']['comment'] = 'something';
$array['image']['tag'] = 'happy';
$array['image']['colors']['blue'] = '12345';
If I have the path to each element in a string, how can I set or get the array value?
e.g where $path = 'image/colors/blue'; the below function should return 12345
function get_array($array, $path)
{
//what goes here?
}
function set_array($array, $path, $value)
{
//what goes here?
}
Try this:
$arr = array('a' => 'A', 'b' => array('c' => 'C', 'd' => array('e'=>'E')));
function read_array($array, $path)
{
if($pos = strpos($path, '/') !== false){
$key = substr($path, 0, $pos);
$restOfKey = substr($path, $pos + 1);
return read_array($array[$key], $restOfKey);
} else {
$key = $path;
return $array[$key];
}
}
echo read_array($arr, 'a'); //A
echo read_array($arr, 'b/c'); //C
echo read_array($arr, 'b/d/e'); //E
You should of course add error checking and all.
Try this. Very basic but it might provide you a jump off point:
$array['image']['comment'] = 'something';
$array['image']['tag'] = 'happy';
$array['image']['colors']['blue'] = '12345';
function get_array($array, $path) {
if(strpos('/', $path) > 0) {
list($first, $second, $third) = explode('/', $path);
return $array[$first][$second][$third];
}
}
get_array($array, 'image/colours/blue');
You could just call split or explode on the path string and insert the individual values into each dimension. This isn't unbounded, but i'm assuming you have a rough idea with this example of the depth this could go.
function getPathVal($array, $path) {
$path_array = explode("/", $path);
$cnt = count($path_array);
if ($cnt == 3) {
return $array[$path_array[0]][$path_array[1]][$path_array[2]];
} else if ($cnt == 2) {
return $array[$path_array[0]][$path_array[1]];
} else if ($cnt == 1) {
return $array[$path_array[0]];
} else {
return "";
}
}

solve a bad Url to clean Url in php?

when I spide a website ,I got a lot of bad url like these.
http://example.com/../../.././././1.htm
http://example.com/test/../test/.././././1.htm
http://example.com/.//1.htm
http://example.com/../test/..//1.htm
all of these should be http://example.com/1.htm.
how to use PHP codes to do this ,thanks.
PS: I use http://snoopy.sourceforge.net/
I get a lot of repeated link in my database , the 'http://example.com/../test/..//1.htm' should be 'http://example.com/1.htm' .
You could do it like this, assuming all the urls you have provided are expected tobe http://example.com/1.htm:
$test = array('http://example.com/../../../././.\./1.htm',
'http://example.com/test/../test/../././.\./1.htm',
'http://example.com/.//1.htm',
'http://example.com/../test/..//1.htm');
foreach ($test as $url){
$u = parse_url($url);
$path = $u['scheme'].'://'.$u['host'].'/'.basename($u['path']);
echo $path.'<br />'.PHP_EOL;
}
/* result
http://example.com/1.htm<br />
http://example.com/1.htm<br />
http://example.com/1.htm<br />
http://example.com/1.htm<br />
*/
//or as a function #lpc2138
function getRealUrl($url){
$u = parse_url($url);
$path = $u['scheme'].'://'.$u['host'].'/'.basename($u['path']);
$path .= (!empty($u['query'])) ? '?'.$u['query'] : '';
return $path;
}
You seem to be looking for a algorithm to remove the dot segments:
function remove_dot_segments($abspath) {
$ib = $abspath;
$ob = '';
while ($ib !== '') {
if (substr($ib, 0, 3) === '../') {
$ib = substr($ib, 3);
} else if (substr($ib, 0, 2) === './') {
$ib = substr($ib, 2);
} else if (substr($ib, 0, 2) === '/.' && ($ib[2] === '/' || strlen($ib) === 2)) {
$ib = '/'.substr($ib, 3);
} else if (substr($ib, 0, 3) === '/..' && ($ib[3] === '/' || strlen($ib) === 3)) {
$ib = '/'.substr($ib, 4);
$ob = substr($ob, 0, strlen($ob)-strlen(strrchr($ob, '/')));
} else if ($ib === '.' || $ib === '..') {
$ib = '';
} else {
$pos = strpos($ib, '/', 1);
if ($pos === false) {
$ob .= $ib;
$ib = '';
} else {
$ob .= substr($ib, 0, $pos);
$ib = substr($ib, $pos);
}
}
}
return $ob;
}
This removes the . and .. segments. Any removal of any other segment like an empty one (//) or .\. is not as per standard as it changes the semantics of the path.
You could do some fancy regex but this works just fine.
fixUrl('http://example.com/../../../././.\./1.htm');
function fixUrl($str) {
$str = str_replace('../', '', $str);
$str = str_replace('./', '', $str);
$str = str_replace('\.', '', $str);
return $str;
}

Categories