从带括号的字符串中删除 url / 域
Removing url / domain from string with parentheses
有什么方法可以从带括号的给定字符串中查找并删除 url 或域?
示例:(like someurl.com) 应该是 (like) and [like someurl.com] 成为 [like] ... also [like someurl.com/path/something.html] 应该是 [like]
也许有人可以帮我提供代码来执行此操作。
$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 . ')';
您需要 Regular Expression (regx) 此处
$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
有什么方法可以从带括号的给定字符串中查找并删除 url 或域?
示例:(like someurl.com) 应该是 (like) and [like someurl.com] 成为 [like] ... also [like someurl.com/path/something.html] 应该是 [like]
也许有人可以帮我提供代码来执行此操作。
$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 . ')';
您需要 Regular Expression (regx) 此处
$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