Elcodi\Plugin\FacebookBundle\Services\FacebookUsernameCleaner::clean PHP Method

clean() public method

Cleans a facebook url username to save only the username
public clean ( string $facebookUsername ) : string
$facebookUsername string The facebook url or username
return string A facebook username
    public function clean($facebookUsername)
    {
        $regex = "/(?:^https?:\\/\\/(?:www\\.)?facebook\\.com\\/)?([^?\\/]+)(.)*\$/";
        return preg_replace($regex, "\$1", $facebookUsername);
    }

Usage Example

 /**
  * Cleans the facebbok username.
  *
  * @param PreUpdateEventArgs $args
  */
 public function preUpdate(PreUpdateEventArgs $args)
 {
     $entity = $args->getEntity();
     if ($entity instanceof Plugin && $entity->getNamespace() == 'Elcodi\\Plugin\\FacebookBundle\\ElcodiFacebookBundle') {
         $facebookAccount = $entity->getConfiguration()->getFieldValue('facebook_account');
         $entity->getConfiguration()->setFieldValue('facebook_account', $this->facebookUsernameCleaner->clean($facebookAccount));
     }
 }
FacebookUsernameCleaner