-3

I have a variable $name which can sometimes contain spaces and special characters like '%' '&' etc.how can I remove all of those using regex or in any other way?

 */
public function handle()
{
    $urls = Business::pluck('ical');
    $names = Business::pluck('name');
    foreach ($urls as $url) {
        foreach ($names as $name) {
            $test= explode("\n", $name);
            dd($test);
        $response = Curl::to($url)
            ->download('ical/'.$name.'.ics');
    }   
Przemek Wojtas
  • 1,311
  • 5
  • 26
  • 51

1 Answers1

0

Use the preg_replace() function of PHP.

$name = preg_replace('/[^\w\d]+/', '', $name);

The regex /[^\w\d]+/ matches all white space and special characters.

https://www.tinywebhut.com/regex/2

Andreas
  • 1,691
  • 1
  • 15
  • 34
Saral
  • 1,087
  • 1
  • 8
  • 18