Detect and remove any url from string using php by examples

To remove any website url in a string, you must know how to detect them, and then can remove.
Consider this string:
“M A N N I N G Mark Fisher Jonas Partner Marius Bogoevici Iwein Fuld FOREWORD BY Rod Johnson IN ACTION https://www.freeaibook.com Spring Integration in Action www.freeaibook.com www.programmingbyexamples.com Spring Integration in Action MARK FISHER JONAS PARTNER MARIUS BOGOEVICI”
There are three website urls:
https://www.freeaibook.com

www.freeaibook.com

www.programmingbyexamples.com
or any other website urls.

Examples:

1.Detect and remove any url from string using php

function removeURLS($str=""){

//$str="M A N N I N G Mark Fisher Jonas Partner Marius Bogoevici Iwein Fuld FOREWORD BY Rod Johnson IN ACTION https://www.freeaibook.com Spring Integration in Action www.freeaibook.com www.programmingbyexamples.com Spring Integration in Action MARK FISHER JONAS PARTNER MARIUS BOGOEVICI";

$tag='@(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))@';

preg_match_all($tag,$str,$r);

//print_r($r);

if(isset($r[1])){

$urls=$r[1];

$len=count($urls);

for($i=0;$i<$len;$i++){

$u=$urls[$i];

if($u!=""){

$str=str_ireplace($u,'',$str);

}

}

}

return $str;

}

Learn php array array_slice() by examples

Description:

array array_slice ( array $array , int $offset [, int $length = NULL [, bool $preserve_keys = FALSE ]] )

Get a slice of the array by offset and length

Parameters:

$offset:
Offset in the array, If offset is negative, the sequence will start that far from the end of the array

$length:
The number of slice

Examples:

1.Get a slice of a array

<?php
$arr = array(1,3,4,5,5)
$result = array_slice($arr, 0, 4);
print_r($result);
?>

Test python variable value is True or False by examples

Description:

To test a variable value is True or False, you should know what value in python is False.

Any object can be tested for truth value, for use in an if or while condition or as operand of the Boolean
operations below. The following values are considered false:

• None
• False
• zero of any numeric type, for example, 0, 0L, 0.0, 0j.
• any empty sequence, for example, ”, (), [].
• any empty mapping, for example, {}.
• instances of user-defined classes, if the class defines a __nonzero__() or __len__() method, when that method returns the integer zero or bool value False.

Examples:

1.Test an list is empty or not

If no any elements in a list, it is empty

Define an empty list

>>> empty_list = []
>>> bool(empty_list)
False

Define a none empty list

>>> list = [1, 2, 3]
>>> bool(list)
True

Learn python int() by examples

Description:

class int(x=0)
class int(x, base=10)

Get an integer object constructed from a number or string x, or return 0 if no arguments are given

Parameters:

x: an integer or string

Examples:

1.Convert an integer string to an integer

if no arguments, it return 0.

>>> int()
0
>>> int('45')
45

2.Convert hexadecimal string to integer

>>> int('0x10',16)
16

How to get hexadecimal string of an integer?

Learn python hex() by examples

Learn python hex() by examples

Description:

hex(x)

Convert an integer or long number to a lowercase hexadecimal string prefixed with “0x”

Examples:

1.Get hexadecimal string of integer

>>> hex(55)
'0x37'
>>> hex(16)
'0x10'
>>> hex(100)
'0x64'

2.Get hexadecimal string of long integer

>>> hex(100L)
'0x64L'
>>> hex(16L)
'0x10L'

You may find the return string of integer and long integer is not the same.

Learn python chr() by examples

Description:

chr(i)

Return a string of one character whose ASCII code is the integer i and i is 0-255

Examples:

1.Get ascll char by integer ascll code

>>> chr(90)
'Z'
>>> chr(0)
'\x00'
>>> chr(255)
'\xff'

Notice: when i is not in 0-255, chr() function will get a ValueError

>>> chr(256)
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
ValueError: chr() arg not in range(256)

Learn python basestring() by examples

Description:

basestring()

It is the superclass for str and unicode. It cannot be called or instantiated, we can check an object is an instance of str or unicode.

Examples:

1.Check an variable is str or not

>>> i = 5
>>> isinstance(i, basestring)
False

It means integer i is not an str object.

>>> s = 'good'
>>> isinstance(s, basestring)
True

It means s is a str object.

Learn python any() by examples

Description:

any(iterable)

Return True if any element of the iterable is true. If the iterable is empty, return False. Equivalent to:

def any(iterable):
 for element in iterable:
   if element:
     return True
   return False

It means one of elements in iterable is True, return True.

Examples:

1.Check there no exist zero in a list or not

>>> list = [0, 0, 0, 0]
>>> any(list)
False
>>> list = [1, 0, 0, 0,]
>>> any(list)
True