Home>

I have code like below and want to convert a snake case such as $building_id to camel.

  public function hoge ($building_id, $owner_member_id)
    {
        $query = Hoge :: query ();
        $query = $query->where ('building_id', $building_id);
        $query = $query->where ('owner_member_id', $owner_member_id);
        return $query->first ();
    }

I would simply replace it with "(_ | ^) (.)" =>"\ U $2".

where ('owner_member_id',


I want to exclude places that start with where, followed by camel case, and end with "',".

where ('owner_member_id',


This line itself matched with "where. * (_) (.). *',"

Specifying the "not included" condition does not work, as shown below.

(_) (.) (?! Where. * (_) (.). *',)

If anyone knows the solution, please let me know

  • Answer # 1

    If you want to convert a variable name starting with "$" to camelcase, you can do it with the following regular expression.
    However, it is impossible to replace all with one replacement, but you can replace by repeating the number of "_".
    Before replacement:

    (\ $[a-z] +) _ ([a-z] +)


    After replacement:

    $1 \ u $2


    Example:

    // Before replacement
    Public function hoge ($building_id, $owner_member_id)
      // First time
    Public function hoge ($buildingId, $ownerMember_id)
      // Second time
    Public function hoge ($buildingId, $ownerMemberId)