Search code examples
phppreg-replacepreg-match

how to remove the particular string using preg match in php


I need to remove the v. from the vairable how can i do that ?

$variable='v.vm_name,p.companyid';

Expected Output

$variable='vm_name,companyid';

how can i achive this using preg_match or preg_replace something.

The value will be dynamic something like this

 $variable='b.vm_name,tt.companyid';

Solution

  • Try this:

    $newstr = preg_replace("/(?:\w\.|\w\w\.)/", "$2", $variable);
    

    It will remove one or two leading chars and a dot.

    Or simply : $newstr = preg_replace("/(?:\w+\.)/", "$2", $variable);