-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,231
  • 4
  • 22
  • 47

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,051
  • 11
  • 26
Saral
  • 1,072
  • 1
  • 7
  • 13