如何从php中的字符串中删除特殊字符和url、链接


how to remove special characters and url , link from a string in php

嗨,我正试图通过删除特殊字符和URL(如果存在)来清理字符串,所以我使用preg_replace进行同样的操作,但如果没有成功,任何人都可以指导我完成这项操作。

<?php
    $string = "Design a Butterfly Mobile in your colors or consider this orb for your nursery. As a unique hanging nursery mobile this piece of butterfly art lasts thru decor changes as your baby enters the toddler years, teen years and then some. The iridescent butterflies are gently scattered thru out this mobile adding a bit of sparkle to your crib mobile. So many colors and sizes to view are here: www.Etsy.com/shop/ButterflyOrbs This ButterflyOrb is a Large size and has: ~96 fabric butterflies in hot pink, pink, white and a touch of iridescent. ~arrives ready to hang. ~a clear hanging line is attached and this allows for the mobile to appear to be floating in the air. It is a Large ButterflyOrb with a span of 16 inches in diameter. The orb mobile moves in gentle circles and will do so in the least of a breeze. Need help designing happy to do so~ just Contact ButterflyOrbs. Read to place your Custom Order, you may do so here: https://www.etsy.com/listing/65078448/nursery-decor-butterfly-nursery?ref=shop_home_active_2 Made to Order.";
//echo $string;
$val = clean($string);
//echo "after cleaning=".$val;
function clean($string) {
   //$string = str_replace(' ', '-', $string); .
    $replace = '"(https?://.*)(?=;)"';
    $output = preg_replace($replace, '', $string);
    $string =  preg_replace('/[^A-Za-z0-9' '']/', '', $output);
   //$string =  preg_replace('/[^A-Za-z0-9' ]/', ' ', $string); 
   $string =  preg_replace('/ /', ' ', $string);
    return strtolower($string);
}
?>

如下更改clean函数。

function clean($string) {
   $string =  preg_replace("~'b(?:https?://(?:www'.)?|www'.)'S+|[^A-Za-z0-9 ']~", '', $string);
    $string =  preg_replace('/ +/', ' ', $string);
    return strtolower($string);
}

演示