【问题标题】:how to remove special characters and url , link from a string in php如何删除特殊字符和url,从php中的字符串链接
【发布时间】:2015-02-24 12:27:35
【问题描述】:

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

【问题讨论】:

  • 您的预期输出是什么?为什么你需要这个preg_replace('/ /', ' ', $string);
  • 检查THIS也许对你有帮助。
  • 基本上我正在尝试使用 pspell 检查字符串的拼写,如果我不清理显示大写字母、https 和如果任何单词以。因此,为了消除所有这些歧义,我什至在发送字符串之前就对其进行了清理
  • 您要删除此www.Etsy.com/shop/ButterflyOrbs 链接吗?
  • @imarcelocc 我试过了,我想从我卡住的字符串中删除 url,但我可以删除特殊字符

标签: php string preg-replace preg-match-all regex-negation


【解决方案1】:

如下更改您的 clean 函数。

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

DEMO

【讨论】:

  • 它有点用,但对于像“quot”这样的少数人来说它没有用,我想通了谢谢
  • 您好,您还可以指导我如何删除同一个 preg_replace 中以数字开头的字符串
  • $string = preg_replace('/[0-9]+/', '', $string);
  • $string = preg_replace('/^[0-9]+/', '', $string); ,这会删除开头出现的所有数字。
猜你喜欢
  • 2011-08-29
  • 1970-01-01
  • 2012-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-11
  • 2016-01-23
相关资源
最近更新 更多