2

i want to check if a user has a domain or website in his name. Much times, user will make advertise for own or other sites on that way.

So i want to replace the URL than with *

I found that

$url = 'Testusername google.com';
$regex = "((https?|ftp)\:\/\/)?"; // SCHEME 
    $regex .= "([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?@)?"; // User and Pass 
    $regex .= "([a-z0-9-.]*)\.([a-z]{2,3})"; // Host or IP 
    $regex .= "(\:[0-9]{2,5})?"; // Port 
    $regex .= "(\/([a-z0-9+\$_-]\.?)+)*\/?"; // Path 
    $regex .= "(\?[a-z+&\$_.-][a-z0-9;:@&%=+\/\$_.-]*)?"; // GET Query 
    $regex .= "(#[a-z_.-][a-z0-9+\$_.-]*)?"; // Anchor 

       if(preg_match("/^$regex$/i", $url)) // `i` flag for case-insensitive
       {     
               $url = str_replace($url, $url, '*');
               echo $url;
               return true; 
       } else {
           echo $url;
       }

It works good to find the url, but how to replace only the domain in that name to * and not everything?

So i want

Testusername *
txen
  • 135
  • 1
  • 10
  • 1
    Check [preg_replace()](http://php.net/preg_replace). So probably something like `preg_replace("/^$regex$/i", $url, '*');` – icecub Aug 15 '18 at 17:51
  • _or_ ... just don't allow characters like `:/` or phrases like `www.` / `.com` in usernames. Ultimately it doesn't matter because people will find a way of getting around your rules. e.g. `go-to-this-site-dot-co-dot-uk` would be a valid username according to your rules. – SierraOscar Aug 15 '18 at 17:59

3 Answers3

1

To replace a regular expression, use preg_replace instead of preg_match and use groups to extract only the valid parts of the username.

$username = 'Testusername google.com';
$regex = '/^(.*?)';
$regex .= "((https?|ftp)\:\/\/)?"; // SCHEME 
$regex .= "([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?@)?"; // User and Pass 
$regex .= "([a-z0-9-.]*)\.([a-z]{2,3})"; // Host or IP 
$regex .= "(\:[0-9]{2,5})?"; // Port 
$regex .= "(\/([a-z0-9+\$_-]\.?)+)*\/?"; // Path 
$regex .= "(\?[a-z+&\$_.-][a-z0-9;:@&%=+\/\$_.-]*)?"; // GET Query 
$regex .= "(#[a-z_.-][a-z0-9+\$_.-]*)?"; // Anchor 
$regex .= '(.*?)$/i';
$username = preg_replace($regex, '$1 * $13', $username);
echo $username; // Testusername *

If you need to know that the username contained a url and a replacement was made, you can use the $count argument to determine how many replacements occurred.

$count = 0;
$username = preg_replace($regex, '$1 * $13', $username, -1, $count);
if ($count > 0) {
    // url replaced in username
}
FThompson
  • 28,352
  • 13
  • 60
  • 93
  • @txen Try the updated full version. The first version of my answer had a couple minor issues that would prevent it from working correctly. – FThompson Aug 15 '18 at 18:02
  • @txen See [this phpfiddle](http://phpfiddle.org/lite/code/dych-tq8i) for demonstration. – FThompson Aug 15 '18 at 18:08
  • Thats working! Nice! Can i also add some words that are bad or something? – txen Aug 16 '18 at 20:45
  • @txen Be sure to mark an answer as correct :) Regarding a profanity filter or similar, [this question](https://stackoverflow.com/q/273516/1247781) begins to address the problem but also notes that it is a fairly complex issue to address. – FThompson Aug 17 '18 at 05:54
1

Try using preg_replace() like so:

$url = 'Testusername google.com';
$regex = "((https?|ftp)\:\/\/)?"; // SCHEME 
    $regex .= "([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?@)?"; // User and Pass 
    $regex .= "([a-z0-9-.]*)\.([a-z]{2,3})"; // Host or IP 
    $regex .= "(\:[0-9]{2,5})?"; // Port 
    $regex .= "(\/([a-z0-9+\$_-]\.?)+)*\/?"; // Path 
    $regex .= "(\?[a-z+&\$_.-][a-z0-9;:@&%=+\/\$_.-]*)?"; // GET Query 
    $regex .= "(#[a-z_.-][a-z0-9+\$_.-]*)?"; // Anchor 

     echo preg_replace("/$regex$/i", '*', $url);

more about preg_replace can be found here

Jora
  • 21
  • 3
0

this should do it - you need to separate the username and url, as well as a few other changes.....

$username = 'Testusername google.com';
$regex = "((https?|ftp)\:\/\/)?"; // SCHEME 
$regex .= "([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?@)?"; // User and Pass 
$regex .= "([a-z0-9-.]*)\.([a-z]{2,3})"; // Host or IP 
$regex .= "(\:[0-9]{2,5})?"; // Port 
$regex .= "(\/([a-z0-9+\$_-]\.?)+)*\/?"; // Path 
$regex .= "(\?[a-z+&\$_.-][a-z0-9;:@&%=+\/\$_.-]*)?"; // GET Query 
$regex .= "(#[a-z_.-][a-z0-9+\$_.-]*)?"; // Anchor 

   if(preg_match("/^$regex$/i", $username,$matches)) // `i` flag for case-insensitive
   {     
        foreach($matches as $key => $url){
           $username = str_replace($url, '*', $username);
         }
         echo $username;
         return true; 
   } else {
       echo $username;
   }
Apps-n-Add-Ons
  • 2,026
  • 1
  • 17
  • 28