2

Is there any way to find and remove the url or domain from given string with parentheses ?

example: (like someurl.com) should be (like) and [like someurl.com] to become [like] ... also [like someurl.com/path/something.html] should be [like]

maybe someone can help me with a code to do this.

AZinkey
  • 5,209
  • 5
  • 28
  • 46
radu
  • 23
  • 3

2 Answers2

0
$str = "(like someurl.com)";
$index_dot =  $index_space_before_dot = $index_after_dot = -1;
for($i=0; $i<strlen($str); $i++){
    if($str[$i] == '.'){
        $index_dot = $i;
    }

    if($str[$i] == ' '  && $index_dot == -1 && $index_space_before_dot == -1){
        $index_space_before_dot = $i;
    }elseif ($str[$i] == ' '  && $index_dot > -1){
        $index_space_before_dot = $i;
    }
}

$str = substr($str, 0, $index_space_before_dot);
echo $str . ')'; 
Haziq Ahmed
  • 87
  • 1
  • 6
0

You need a Regular Expression (regx) here

$string_with_url = 'lorem (like GOoogle.COM someurl.com/path/something.html ) lipsum';

$string_without_url = preg_replace("/(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-zA-Z0-9]+([\-\.]{1}[a-zA-Z0-9]+)*\.[a-zA-Z]{2,5}(:[0-9]{1,5})?(\/.+[a-zA-Z\/]\s)?/", "", $string_with_url);

echo $string_without_url; // lorem (like ) lipsum
AZinkey
  • 5,209
  • 5
  • 28
  • 46
  • This works great on lowercase domains, uppercase ones are not replaced, or if an domain is : someDomain.com is replacing only omain.com and it will becode someD – radu Oct 18 '17 at 06:17