-1

Hi i am trying to cleanse a string by removing special characters and URL if present , so i am using preg_replace for the same , but not successful in doing the same can any one guide me through this .

<?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);
}
?>
I'm Geeker
  • 4,642
  • 5
  • 19
  • 40
user3470929
  • 1,148
  • 15
  • 15
  • What's your expected output? Why you need this `preg_replace('/ /', ' ', $string);` ? – Avinash Raj Feb 24 '15 at 12:30
  • Check [THIS](http://stackoverflow.com/questions/14114411/remove-all-special-characters-from-a-string) maybe it will help you. – lmarcelocc Feb 24 '15 at 12:34
  • basically i am trying to check the spellings of the string using pspell for which i have to send this data to pspell function if i don't clean the data it shows errors for capital letters , https, and if any word ends with. so to remove all those ambiguity i am cleaning the string even before sending it – user3470929 Feb 24 '15 at 12:35
  • did you want to remove this `www.Etsy.com/shop/ButterflyOrbs` link? – Avinash Raj Feb 24 '15 at 12:36
  • @imarcelocc i tried it , i want to remove url from string thats where i am stuck , i am able to remove the special characters though – user3470929 Feb 24 '15 at 12:37
  • @Avinash Raj yes i want to remove www.Etsy.com/shop/ButterflyOrbs and https://www.etsy.com/listing/65078448/nursery-decor-butterfly-nursery?ref=shop_home_active_2 from string – user3470929 Feb 24 '15 at 12:38
  • i don't know why the guy who down voted me thinks i didn't do enough research , its unfair to downvote for a genuine question which has code in it – user3470929 Feb 24 '15 at 12:57

1 Answers1

1

Change your clean function like below.

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

DEMO

Avinash Raj
  • 160,498
  • 22
  • 182
  • 229