Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php中删除字符串中的特殊字符和url、链接_Php_String_Preg Replace_Preg Match All_Regex Negation - Fatal编程技术网

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

如何在php中删除字符串中的特殊字符和url、链接,php,string,preg-replace,preg-match-all,regex-negation,Php,String,Preg Replace,Preg Match All,Regex Negation,您好,我试图通过删除特殊字符和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 l

您好,我试图通过删除特殊字符和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);
}
?>

更改
清洁功能,如下所示

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

您的预期产量是多少?为什么需要这个
preg_replace('/','$string)?检查可能会对您有所帮助。基本上,我正在尝试使用pspell检查字符串的拼写,如果我不清理显示大写字母、https错误的数据,以及任何以大写字母结尾的单词,我必须将此数据发送到pspell函数。所以,为了消除所有这些歧义,我甚至在发送之前就清理了字符串。你想删除这个
www.Etsy.com/shop/ButterflyOrbs
link吗?@imarcelocc我试过了,我想从我卡住的字符串中删除url,我可以删除特殊字符,虽然它有点工作,但像“quot”这样的很少它不起作用,我想出来了,谢谢你好,你也可以指导我如何删除以数字开头的字符串,在相同的preg_replace$string=preg_replace('/[0-9]+/','$string)<代码>$string=preg_replace('/^[0-9]+/',''$string),这将删除开头出现的所有数字。