有没有办法从括号中找到并删除给定字符串中的url或域?
例子:(像someurl.com)应该(像)和[像someurl.com]成为[喜欢] ...也[像someurl.com/path/something.html]应该[像] < / p>
也许有人可以帮我一个代码来做这件事。
答案 0 :(得分: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 . ')';
答案 1 :(得分:0)
您需要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